/******************************************************************************
*******************************************************************************
Created: 2/18/2022

gerrymander_Restat.do


Authors: Dahyeon Jeong and Ajay Shenoy

Replication Codes for:

	"The Targeting and Impact of Partisan Gerrymandering: 
	    Evidence from a Legislative Discontinuity"
		
*******************************************************************************
*******************************************************************************/


clear
cap cd "[PATH]" // the path where the analysis folder is stored

//Labels
global redel 	"High-Stakes Election"
global nonel 	"Low-Stakes Election"

global redelsh 	"High-Stakes"
global nonelsh	"Low-Stakes"

global xlabmaj	`""Probability Democrats Won a" "Majority ({bf:Previous} Election)""'

global runlabdem `""Seats Won by Democrats in State Assembly" "During Redistricting Election (% of total, 0=50%)""'
global runlabmaj `""Seats Won in {bf:Current} Election" "by Party that {bf:Previously} Held Majority" "As % of total, relative to 50% threshold""'
global runlabinc `""Democratic Incumbents (Seats Won, {bf:Previous} Election)" "As % of total, relative to 50% threshold""'

global seatcol navy%10
global sharecol red


global runlabpost `" "Seats Won by Democrats in State Assembly" "Prior Redistricting Election (% of total, 0=50%)" "'
global runlabpre `" "Seats Won by Democrats in State Assembly" "Upcoming Redistricting Election (% of total, 0=50%)" "'

//Sample Restrictions
global twocandset "keep if (ndem == 1 & nrep == 1)"

global ci .975
global cilabel = round((1-((1-${ci})*2))*100)
	
/*******************************************************************************

					  Tables and Figures in the Main Text

[Main]
Figure 1: State Assembly Members Vote for the Redistricting Bill when their Party Holds a Majority
Figure 2: Schedule of Redistricting
Figure 3: The Selection Effect and Causal Effect are Visible in the Data
Figure 4: Figure 3 is Not Driven by Choice of Bandwidth
Figure 5: The Selection Effect on Vote Shares Does Not Vanish after Redistricting
Figure 6: Difference-in-Discontinuity Estimate of Causal Effect of Redistricting
Figure 7: Republican Votes are Converted to Seats at a Less Favorable Rate when Democrats Control the Assembly

Table 1: Estimates of the Selection Effect
Table 2: Main Results: Difference-in-Discontinuity Estimates Causal Effect of Dem. Control in the State Assembly on First U.S. House Election After Redistricting
Table 3: Evidence of Racial Gerrymandering


[Appendix]
Figure 8: The Effect of Gerrymandering Grows Larger and More Persistent
Figure 9: Simulation Example: A Negative Shock to Support for Republicans Nullifies the Gains from Gerrymandering
Figure 10: Simulated Effects: Neutral National Shock
Figure 11: Histogram of Seats Won by Democrats by State-Redistricting Events
Figure 12: The Selection Effect on Vote Shares Does Not Vanish after Redistricting (State-Level Regressions)
Figure 13: African Americans are More Likely
Figure 14: Conditional on Being Moved, Where Are African Americans Moved?
Figure 15: No Evidence of a Greater Need to Redistricting African Americans on One Side of the Threshold
Figure 16: Effect of Packing and Cracking on Distribution of Political Opponents
Figure 17: Robustness to Bandwidth: Table 1, Specification 4
Figure 18: Robustness to Bandwidth: Table 3, Columns 1 and 2
Figure 19: Robustness to Bandwidth: Table 3, Column 3
Figure 20: Robustness to Bandwidth: Figure 16.B
Figure 21: Robustness to Bandwidth: Table 3, Columns 4 and 5
Figure 22: The Main Results Hold with the Alternative Dataset
Figure 23: The Main Results Hold after Dropping Maine

Table 4: Descriptive Statistics, Election Outcomes
Table 5: Descriptive Statistics, District Demographics
Table 6: States and Years in Full Sample and In Bandwidth
*******************************************************************************/

/************
 Figure 1
*************/

// Get number of democrats and republicans actually in session (as opposed to elected)
use "dta/state_legislature_detail.dta", clear
keep if year == 2011
keep statename hs_dem_in_sess hs_rep_in_sess

tempfile insess
save `insess'

//Get running variable and representatives in session
use "dta/stateleg/partisan_balance_lh.dta", clear
keep if red_election

keep if year_redistrict == 2011
rename year sess_year

keep statename demhs2 
tempfile run2
save `run2'


//Get votes for most recent redistricting
import excel using "raw data/voting/voting_redistrict_20151009.xlsx", first clear

rename year year_redistrict
sort statename year_redistrict bill

#delimit ;
destring hs_dem_no hs_dem_yes hs_ind_no hs_ind_yes hs_rep_no hs_rep_yes sen_dem_no 
sen_dem_yes sen_ind_no sen_ind_yes sen_rep_no sen_rep_yes con_sen_dem_no con_sen_dem_yes 
con_sen_ind_no con_sen_ind_yes con_sen_rep_no con_sen_rep_yes con_hs_dem_no 
con_hs_dem_yes con_hs_ind_no con_hs_ind_yes con_hs_rep_no con_hs_rep_yes hs_tot_no 
hs_tot_yes sen_tot_no sen_tot_yes con_hs_tot_no, replace;
#delimit cr

keep if year == 2011

merge m:1 statename using `insess'
drop if _m==2
drop _m

merge m:1 statename using `run2'
drop if _m==2


//Fraction of republicans or democrats who vote yes in house
//Note this is as a fraction actually in session, not those elected
gen fracrep = hs_rep_yes/ hs_rep_in_sess * 100
gen fracdem = hs_dem_yes/ hs_dem_in_sess * 100


#delimit ;
binplot fracdem, x(demhs2) bw(4) lineone linebreak 
			opt(`"
			legend(off) 
			xtitle(${runlabdem}, size(large) ) 
			ytitle("Percentage Voting Yes in Assembly", size(large) )
			title("Democrats", size(vlarge) ) name(g1)
			"')
			;
binplot fracrep, x(demhs2) bw(4) lineone linebreak 
			opt(`"
			legend(off) 
			xtitle(${runlabdem}, size(large) ) 
			ytitle("") 
			title("Republicans", size(vlarge) ) name(g2)
			"')
			;
#delimit cr

graph combine g1 g2, graphregion(color(white) fcolor(white)) ysize(3)
graph drop g1 g2

cap mkdir graphs/background
graph export "graphs/background/partylines.pdf", replace



/************
 Figure 2 - no replication code. The figure made in PPT
*************/
//graphs/background/redistricting_cycle_mod2

/************
 Figure 3
*************/
	
	/************************************************************************
	Pre-Trend
	************************************************************************/
	
	
	
	local out rep_win		//Outcome
	local bw = 18			//Bandwidth
	local binsize = 3		//Bin size
	local deg = 1			//Polynomial degree
	local run demhs2			//Running variable
	
	use "dta/gerrymander/election_level_house.dta", clear
	
	
	
	
	
	
	local set1 "keep if fed < year_red - 10 + 5"
	local set2 "keep if fed >= year_red - 10 + 5"
	
	local title1 "a)     6 to 10 Years Before Redistricting"
	local title2 "b)     1 to 5 Years Before Redistricting"
	
	
	local ytit1 "in {bf:U.S. House of Reps}  "
	
	foreach s in 1 2 {
		preserve
		//Sample: Elections in the specified year
		keep if sample == 1 
	
		$twocandset
		
		
	
		//keep if fed >= 1972
	
	
		`set`s''
	
		//Cluster (for standard errors)
		egen clust = group(year_redistrict state_id)
	
		//Indicator for control of the assembly
		gen control = (`run' >= 0) if !mi(`run')
	
		//Restrict to bandwidth
		keep if abs(`run') < `bw'
	
		//Generate control function
		forvalues p=1/`deg' {
			gen poly`p' = `run'^`p'
		}
	
	
		//Run regression on micro data
		reg `out' poly* control c.control#c.poly* , cluster(clust)
	
		//Size of discontinuity and standard error
		local b 	= string( _b[control], "%9.3f" )
		local se 	= string( _se[control], "%9.3f" )
	
		//Round to construct bins
		gen bin = `run' - mod(`run',`binsize')
	
		//Take bin averages (and counts)
		gen nonmi = e(sample)
		collapse `out' (sum)nobs=nonmi, by(bin)
		
		//Recenter the running variable on the midpoints of the bins
		gen `run' 					= bin+`binsize'/2
		gen control					= `run' > 0
	
	
		forvalues p=1/`deg' {
			gen poly`p' = `run'^`p'
		}
		predict pred
	
	
		#delimit;
		twoway 
		(scatteri 	.25 -19 "Republican Control"
						.25 1 "Democratic Control", ms(i) mlabsize(large) mlabcolor(gs10) )
		(scatter `out' `run' ${useweight}, msize(2) mc(navy) )				//Dots
		(line pred `run' if control)  				//Fit: Republican control
		(line pred `run' if !control) 				//Fit: Democratic control
		,
		text(.75 -8 "Discontinuity", size(medlarge) )  text(.75 1 "`b'" "(`se')", size(medlarge) ) 
		graphregion(color(white)) legend(off) xline(0, lc(gs14) lp(dash) ) 
		xtitle("", size(large) )
		ytitle("`ytit`s''", size(vlarge) ) ylabel(.2 (.2) .8)
		title("`title`s''", size(vlarge) ) name(pre`s') xline(0, lc(gs14) lp(dash) )
		;
		#delimit cr
	
		
		sum nobs
		pause
		
		restore
	}
	
	
	
	
	/************************************************************************
	Post-Trend
	************************************************************************/
	
	
	
	local out rep_win		//Outcome
	local bw = 18			//Bandwidth
	local binsize = 3		//Bin size
	local deg = 1			//Polynomial degree
	local run demhs2			//Running variable
	
	use "dta/gerrymander/election_level_house_post.dta", clear
	
	
	
	
	
	
	local set1 "keep if fed == year_red + 1"
	local set2 "keep if fed >= year_red + 7"
	
	local title1 "c)     1 Year After Redistricting"
	local title2 "d)     7 to 9 Years After Redistricting"
	
	local ytit1 "Probability Republican Wins"
	
	//local tmp `"${runlabdem}"'
	//gettoken xtit1 xtit2: tmp
	local xtit1 " Seats Won by Democrats in {bf:State Assembly}"
	local xtit2 "During Redistricting Election (% of total, 0=50%)    "
	
	
	foreach s in 1 2 {
		preserve
		//Sample: Elections in the specified year
		keep if sample == 1 
	
		$twocandset
		
		
	
		//keep if fed >= 1972
	
	
		`set`s''
	
		//Cluster (for standard errors)
		egen clust = group(year_redistrict state_id)
	
		//Indicator for control of the assembly
		gen control = (`run' >= 0) if !mi(`run')
	
		//Restrict to bandwidth
		keep if abs(`run') < `bw'
	
		//Generate control function
		forvalues p=1/`deg' {
			gen poly`p' = `run'^`p'
		}
	
	
		//Run regression on micro data
		reg `out' poly* control c.control#c.poly* , cluster(clust)
	
		//Size of discontinuity and standard error
		local b 	= string( _b[control], "%9.3f" )
		local se 	= string( _se[control], "%9.3f" )
	
		//Round to construct bins
		gen bin = `run' - mod(`run',`binsize')
	
		//Take bin averages (and counts)
		gen nonmi = e(sample)
		collapse `out' (sum)nobs=nonmi, by(bin)
		
		//Recenter the running variable on the midpoints of the bins
		gen `run' 					= bin+`binsize'/2
		gen control					= `run' > 0
	
	
		forvalues p=1/`deg' {
			gen poly`p' = `run'^`p'
		}
		predict pred
	
		#delimit;
		twoway 
		(scatteri 	.25 -19 "Republican Control"
						.25 1 "Democratic Control", ms(i) mlabsize(large) mlabcolor(gs10) )
		(scatter `out' `run' ${useweight}, msize(2) mc(navy) )				//Dots
		(line pred `run' if control)  				//Fit: Republican control
		(line pred `run' if !control) 				//Fit: Democratic control
		,
		text(.75 -8 "Discontinuity", size(medlarge))  text(.75 1 "`b'" "(`se')", size(medlarge) ) 
		graphregion(color(white)) legend(off) xline(0, lc(gs14) lp(dash) ) 
		xtitle("`xtit`s''", size(vlarge) )
		ytitle("`ytit`s''", size(vlarge) ) ylabel(.2 (.2) .8)
		title("`title`s''", size(vlarge) ) name(post`s') xline(0, lc(gs14) lp(dash) )
		;
		#delimit cr
		
		
		sum nobs
		pause
		
		restore
	
	}
	
	
	graph combine pre1 pre2 post1 post2, graphregion(color(white) fcolor(white) ) rows(2) xsize(7)
	graph drop pre1 pre2 post1 post2
	
	
	cap mkdir graphs/electionresults
	graph export "graphs/electionresults/racelev_evolution.pdf", replace
	

/************
 Figure 4
*************/

//Pre-Trend
local set1 "keep if fed < year_red - 10 + 5"
local set2 "keep if fed >= year_red - 10 + 5"

local title1 "6 to 10 Years Before Redistricting"
local title2 "1 to 5 Years Before Redistricting"

graph drop _all
foreach s in 1 2 {
	clear
	cap postclose rdest
	tempfile rdest
	postfile rdest bw b se nclust using `rdest'
	
	
	local out rep_win		//Outcome
	local deg = 1			//Polynomial degree
	local run demhs2			//Running variable

	use "dta/gerrymander/election_level_house.dta", clear



	
	foreach bw of numlist 8(.5)22 {
		preserve
		//Sample: Elections in the specified year
		keep if sample == 1 

		$twocandset
		
		

		//keep if fed >= 1972


		`set`s''

		//Cluster (for standard errors)
		egen clust = group(year_redistrict state_id)

		//Indicator for control of the assembly
		gen control = (`run' >= 0) if !mi(`run')

		//Restrict to bandwidth
		keep if abs(`run') < `bw'

		//Generate control function
		forvalues p=1/`deg' {
			gen poly`p' = `run'^`p'
		}


		//Run regression on micro data
		reg `out' poly* control c.control#c.poly* , cluster(clust)
		
		local b 		= _b[control]
		local se		= _se[control]
		local nclust	= e(N_clust)
		
		post rdest (`bw') (`b') (`se') (`nclust')
		
		restore
	}

	postclose rdest
		
	use `rdest', clear

	gen bl = b - invt(nclust-1, ${ci})*se
	gen bu = b + invt(nclust-1, ${ci})*se


	local estcol gs8
	#delimit;
	scatter b bl bu bw, sort(bw) connect(direct direct direct) lp(solid dash dash) 
						lc(`estcol' `estcol' `estcol') mc(`estcol' `estcol' `estcol') 
						ms(o p p)  xlabel(8(3.5)22) ylabel(-.3(.1).3)
						 yline(0, lc(black) ) name(pre`s')
						graphregion(color(white)) xtitle("Bandwidth", size(medlarge)) legend(off)
						ytitle("RD Estimate (${cilabel}% CI) ", size(medlarge)) title("`title`s''", size(large) )
						
			;
	#delimit cr
	
	
	

}




//Post-Trend
local set1 "keep if fed == year_red + 1"
local set2 "keep if fed >= year_red + 7"

local title1 "1 Year After Redistricting"
local title2 "7 to 9 Years After Redistricting"

foreach s in 1 2 {
	clear
	cap postclose rdest
	tempfile rdest
	postfile rdest bw b se nclust using `rdest'
	
	
		
	local out rep_win		//Outcome
	local deg = 1			//Polynomial degree
	local run demhs2			//Running variable

	
	use "dta/gerrymander/election_level_house_post.dta", clear



	foreach bw of numlist 8(.5)22 {
		preserve
		//Sample: Elections in the specified year
		keep if sample == 1 

		$twocandset
		
		


		`set`s''

		//Cluster (for standard errors)
		egen clust = group(year_redistrict state_id)

		//Indicator for control of the assembly
		gen control = (`run' >= 0) if !mi(`run')

		//Restrict to bandwidth
		keep if abs(`run') < `bw'

		//Generate control function
		forvalues p=1/`deg' {
			gen poly`p' = `run'^`p'
		}


		//Run regression on micro data
		reg `out' poly* control c.control#c.poly* , cluster(clust)

		local b 		= _b[control]
		local se		= _se[control]
		local nclust	= e(N_clust)
		
		post rdest (`bw') (`b') (`se') (`nclust')

		restore
	}
	
	postclose rdest
	
	use `rdest', clear
	
	gen bl = b - invt(nclust-1, ${ci})*se
	gen bu = b + invt(nclust-1, ${ci})*se
	
	local estcol gs8
	#delimit;
	scatter b bl bu bw, sort(bw) connect(direct direct direct) lp(solid dash dash) 
						lc(`estcol' `estcol' `estcol') mc(`estcol' `estcol' `estcol') 
						ms(o p p)  xlabel(8(3.5)22) ylabel(-.3(.1).3)
						 yline(0, lc(black) ) name(post`s')
						graphregion(color(white)) xtitle("Bandwidth", size(medlarge)) legend(off)
						ytitle("RD Estimate (${cilabel}% CI) ", size(medlarge)) title("`title`s''", size(large))
						
			;
	#delimit cr
}


graph combine pre1 pre2 post1 post2, graphregion(color(white) fcolor(white) ) rows(2) xsize(6)
graph drop pre1 pre2 post1 post2

cap mkdir graphs/appendix
graph export "graphs/appendix/racelev_bw.pdf", replace




/************
 Figure 5
*************/


//Pre-Trend

foreach outcome in win share{
	
	local out rep_`outcome'		//Outcome
	local bw = 18			//Bandwidth
	local binsize = 3		//Bin size
	local deg = 1			//Polynomial degree
	local run demhs2			//Running variable
	
	
	use "dta/gerrymander/election_level_house.dta", clear
	
	local set1 "keep if fed < year_red - 10 + 5"
	local set2 "keep if fed >= year_red - 10 + 5"
		
	local title1 "a)     6 to 10 Years Before Redistricting"
	local title2 "b)     1 to 5 Years Before Redistricting"
	
	local ytit1 "in {bf:U.S. House of Reps}    "
	
	
	
	foreach s in 1 2{
		preserve
		//Sample: Elections in the specified year
		keep if sample == 1 
	
		$twocandset
		
		//keep if fed >= 1972
	
	
		`set`s''
	
		//Cluster (for standard errors)
		egen clust = group(year_redistrict state_id)
	
		//Indicator for control of the assembly
		gen control = (`run' >= 0) if !mi(`run')
	
		//Restrict to bandwidth
		keep if abs(`run') < `bw'
	
		//Generate control function
		forvalues p=1/`deg' {
			gen poly`p' = `run'^`p'
		}
	
	
		//Run regression on micro data
		reg `out' poly* control c.control#c.poly* , cluster(clust)
	
		//Size of discontinuity and standard error
		local b`outcome'`s' 	= string( _b[control], "%9.3f" )
		local se`outcome'`s' 	= string( _se[control], "%9.3f" )
	
		//Round to construct bins
		gen bin = `run' - mod(`run',`binsize')
	
		//Take bin averages (and counts)
		gen nonmi = e(sample)
		collapse `out' (sum)nobs=nonmi, by(bin)
		
		//Recenter the running variable on the midpoints of the bins
		gen `run' 					= bin+`binsize'/2
		gen control					= `run' > 0
	
	
		forvalues p=1/`deg' {
			gen poly`p' = `run'^`p'
		}
		predict pred
	
		gen sample = "`outcome'`s'"
		tempfile `outcome'`s'
		save ``outcome'`s''
		restore
	}

}

use `win1', clear
append using `win2'
append using `share1'
append using `share2'


cap graph drop _all

#delimit;
	twoway 
	(scatteri 	.25 -19 "Republican Control"
					.25 1 "Democratic Control", ms(i) mlabsize(large) mlabcolor(gs10) )
	(scatter rep_win `run' ${useweight} if sample=="win1", msize(2) mc(${seatcol}) )				//Dots
	(scatter rep_share `run' ${useweight} if sample=="share1", msize(2) mcolor(${sharecol}) yaxis(2))				//Dots
	(line pred `run' if control & sample=="win1", lc(${seatcol}))  				//Fit: Republican control
	(line pred `run' if !control & sample=="win1", lc(${seatcol})) 				//Fit: Democratic control
	(line pred `run' if control & sample=="share1", lc(${sharecol}) yaxis(2))  				//Fit: Republican control
	(line pred `run' if !control & sample=="share1", lc(${sharecol}) yaxis(2)) 				//Fit: Democratic control
	, 
	graphregion(color(white)) legend(off) xline(0, lc(gs14) lp(dash) ) 
	xtitle("", size(large) )
	ytitle("`ytit1'", size(large) ) ytitle("", axis(2))  ylabel(.2 (.2) .8) ylabel(.4 (.1) .6, axis(2))
	title("`title1'", size(vlarge) ) name(pre1) xline(0, lc(gs14) lp(dash) )
	text(.58 6.5 "Discontinuity", size(medlarge) yaxis(2) color(${sharecol}))  text(.58 16 "`bshare1'" "(`seshare1')", size(medlarge) yaxis(2) color(${sharecol}) ) 
	;
	#delimit cr

#delimit;
	twoway 
	(scatteri 	.25 -19 "Republican Control"
					.25 1 "Democratic Control", ms(i) mlabsize(large) mlabcolor(gs10) )
	(scatter rep_win `run' ${useweight} if sample=="win2", msize(2) mc(${seatcol}) )				//Dots
	(scatter rep_share `run' ${useweight} if sample=="share2", msize(2) mc(${sharecol}) yaxis(2))				//Dots
		(line pred `run' if control & sample=="win2", lc(${seatcol}))  				//Fit: Republican control
	(line pred `run' if !control & sample=="win2", lc(${seatcol})) 				//Fit: Democratic control
	(line pred `run' if control & sample=="share2", lc(${sharecol}) yaxis(2))  				//Fit: Republican control
	(line pred `run' if !control & sample=="share2", lc(${sharecol}) yaxis(2)) 				//Fit: Democratic control
	, 
	graphregion(color(white)) legend(off) xline(0, lc(gs14) lp(dash) ) 
	xtitle("", size(large) )
	ytitle("", size(large)) ytitle("`ytit1'", axis(2) size(large)) ylabel(.2 (.2) .8) ylabel(.4 (.1) .6, axis(2))
	title("`title2'", size(vlarge) ) name(pre2) xline(0, lc(gs14) lp(dash) )
	text(.58 6.5 "Discontinuity", size(medlarge) yaxis(2) color(${sharecol}))  text(.58 16 "`bshare2'" "(`seshare2')", size(medlarge) yaxis(2) color(${sharecol}) ) 
	;
	#delimit cr	
	

	
//Post-Trend

foreach outcome in win share{
	
	local out rep_`outcome'		//Outcome	
	local bw = 18			//Bandwidth
	local binsize = 3		//Bin size
	local deg = 1			//Polynomial degree
	local run demhs2			//Running variable
	
	use "dta/gerrymander/election_level_house_post.dta", clear
	
	
	local set1 "keep if fed == year_red + 1"
	local set2 "keep if fed >= year_red + 7"
	
	local title1 "c)     1 Year After Redistricting"
	local title2 "d)     7 to 9 Years After Redistricting"
	
	local ytit1 "Probability Republican Wins"
	local ytit2 "Republican Vote Share"
	
	//local tmp `"${runlabdem}"'
	//gettoken xtit1 xtit2: tmp
	local xtit1 " Seats Won by Democrats in {bf:State Assembly}"
	local xtit2 "During Redistricting Election (% of total, 0=50%)    "
	
	
	foreach s in 1 2 {
		preserve
		//Sample: Elections in the specified year
		keep if sample == 1 
	
		$twocandset
		
		//keep if fed >= 1972
	
	
		`set`s''
	
		//Cluster (for standard errors)
		egen clust = group(year_redistrict state_id)
	
		//Indicator for control of the assembly
		gen control = (`run' >= 0) if !mi(`run')
	
		//Restrict to bandwidth
		keep if abs(`run') < `bw'
	
		//Generate control function
		forvalues p=1/`deg' {
			gen poly`p' = `run'^`p'
		}
	
	
		//Run regression on micro data
		reg `out' poly* control c.control#c.poly* , cluster(clust)
	
		//Size of discontinuity and standard error
		local b`outcome'`s' 	= string( _b[control], "%9.3f" )
		local se`outcome'`s' 	= string( _se[control], "%9.3f" )
	
		//Round to construct bins
		gen bin = `run' - mod(`run',`binsize')
	
		//Take bin averages (and counts)
		gen nonmi = e(sample)
		collapse `out' (sum)nobs=nonmi, by(bin)
		
		//Recenter the running variable on the midpoints of the bins
		gen `run' 					= bin+`binsize'/2
		gen control					= `run' > 0
	
	
		forvalues p=1/`deg' {
			gen poly`p' = `run'^`p'
		}
		predict pred
		gen sample = "`outcome'`s'"
		tempfile `outcome'`s'
		save ``outcome'`s''
		restore
	}
}


use `win1', clear
append using `win2'
append using `share1'
append using `share2'


#delimit;
	twoway 
	(scatteri 	.25 -19 "Republican Control"
					.25 1 "Democratic Control", ms(i) mlabsize(large) mlabcolor(gs10) )
	(scatter rep_win `run' ${useweight} if sample=="win1", msize(2) mc(${seatcol}) )				//Dots
	(scatter rep_share `run' ${useweight} if sample=="share1", msize(2) mcolor(${sharecol}) yaxis(2))				//Dots
		(line pred `run' if control & sample=="win1", lc(${seatcol}))  				//Fit: Republican control
	(line pred `run' if !control & sample=="win1", lc(${seatcol})) 				//Fit: Democratic control
	(line pred `run' if control & sample=="share1", lc(${sharecol}) yaxis(2))  				//Fit: Republican control
	(line pred `run' if !control & sample=="share1", lc(${sharecol}) yaxis(2)) 				//Fit: Democratic control
	, 
	graphregion(color(white)) legend(off) xline(0, lc(gs14) lp(dash) ) 
	xtitle("`xtit1'", size(large) )
	ytitle("`ytit1'", size(large) ) ytitle("", axis(2)) ylabel(.2 (.2) .8) ylabel(.4 (.1) .6, axis(2))
	title("`title1'", size(vlarge) ) name(post1) xline(0, lc(gs14) lp(dash) )
	text(.58 6.5 "Discontinuity", size(medlarge) yaxis(2) color(${sharecol}))  text(.58 16 "`bshare1'" "(`seshare1')", size(medlarge) yaxis(2) color(${sharecol}) ) 
	text(.46 -10 "Vote Share", yaxis(2) color(${sharecol}))
	text(.75 -10 "Seats Won", yaxis(1) color(navy%30))
	;
	#delimit cr


#delimit;
	twoway 
	(scatteri 	.25 -19 "Republican Control"
					.25 1 "Democratic Control", ms(i) mlabsize(large) mlabcolor(gs10) )
	(scatter rep_win `run' ${useweight} if sample=="win2", msize(2) mc(${seatcol}) )				//Dots
	(scatter rep_share `run' ${useweight} if sample=="share2", msize(2) mc(${sharecol}) yaxis(2))				//Dots
		(line pred `run' if control & sample=="win2", lc(${seatcol}))  				//Fit: Republican control
	(line pred `run' if !control & sample=="win2", lc(${seatcol})) 				//Fit: Democratic control
	(line pred `run' if control & sample=="share2", lc(${sharecol}) yaxis(2))  				//Fit: Republican control
	(line pred `run' if !control & sample=="share2", lc(${sharecol}) yaxis(2)) 				//Fit: Democratic control
	, 
	graphregion(color(white)) legend(off) xline(0, lc(gs14) lp(dash) ) 
	xtitle("`xtit2'", size(large) )
	ytitle("", size(large) ) ytitle("`ytit2'", axis(2) size(large)) ylabel(.2 (.2) .8) ylabel(.4 (.1) .6, axis(2))
	title("`title2'", size(vlarge) ) name(post2) xline(0, lc(gs14) lp(dash) )
	text(.58 6.5 "Discontinuity", size(medlarge) yaxis(2) color(${sharecol}))  text(.58 16 "`bshare2'" "(`seshare2')", size(medlarge) yaxis(2) color(${sharecol}) ) 
	;
	#delimit cr	
	
		
		
graph combine pre1 pre2 post1 post2, graphregion(color(white) fcolor(white) ) rows(2) xsize(7) imargin(0 0 0 0)
graph drop pre1 pre2 post1 post2


graph export "graphs/electionresults/race_lev_share_evolution.pdf", replace




/************
 Figure 6
*************/

local out rep_win		//Outcome
local bw = 18			//Bandwidth
local deg = 1			//Polynomial degree
local run demhs2		//Running variable

use "dta/gerrymander/election_level_house_qpanel.dta", clear


preserve
//Sample: Elections in the specified year
keep if sample == 1 

$twocandset


//keep if fed >= 1972


//Cluster (for standard errors)
egen clust = group(year_redistrict state_id)

//Indicator for control of the assembly
gen control = (`run' >= 0) if !mi(`run')

//Restrict to bandwidth
keep if abs(`run') < `bw'

//Generate control function
forvalues p=1/`deg' {
	gen poly`p' = `run'^`p'
}


//Run regression on micro data
gen tmod = t+5
reg `out' i.tmod##c.(poly* control c.control#c.poly*) , cluster(clust)


clear
set obs 5
gen t 		= -5 + 2*(_n-1)
gen tmod 	= 2*(_n-1)


gen b 	= 0
gen se 	= 0
foreach tmod of numlist 2(2)8 {
	replace b 	= _b[`tmod'.tmod#c.control] 	if tmod == `tmod'
	replace se 	= _se[`tmod'.tmod#c.control] 	if tmod == `tmod'
}

local nclust = e(N_clust)


#delimit;
serrbar b se t, scale(`=invt(`nclust'-1, ${ci})') lp(dash dash) lc(blue blue)
					mvopt( mc(blue ) ms(S sh sh) )
					yline(0, lc(black) ) xline(0, lc(red) lp(dash) ) xline(2, lc(red) lp(dash) )
					legend(off) graphregion(color(white))
					xtitle("Years Until Redistricting", size(vlarge) )
					ytitle("Change in Probability Republican" "Wins when Democrats Take Control", size(large))
					xlabel(-5(2)3, labsize(vlarge))  ylabel(, labsize(vlarge))
					text(.18 1 "First Election" "After" "Redistricting", color(red) )
					text(.18 -2 "<------------Before Redistricting", color(gs8) )
					text(.18 2.5 "After-->", color(gs8) )
					;
#delimit cr

restore



graph export "graphs/electionresults/dif_in_disc.pdf", replace




/************
 Figure 7
*************/

local trep_share	"Republican Vote Share"
local trep_win		"Fraction of Seats Won by Republicans"
local tconvert		"Conversion Rate (Seats/Share)"

local yrep_share	"Change when Democrats Take Control"

local bw = 18			//Bandwidth
local deg = 1			//Polynomial degree
local run demhs2		//Running variable

use "dta/gerrymander/election_level_house_qpanel.dta", clear



foreach out in rep_share rep_win convert {


	preserve
	//Sample: Elections in the specified year
	keep if sample == 1 

	$twocandset


	//keep if fed >= 1972
		
	collapse (sum) dem_votes rep_votes ind_votes (mean) dem_win rep_win ind_win (first) `run', by(year_redistrict federal_election_year state_id t)
	gen rep_share = rep_votes/(rep_votes+dem_votes+ind_votes)

	gen convert = rep_win / rep_share
	


	//Cluster (for standard errors)
	egen clust = group(year_redistrict state_id)

	//Indicator for control of the assembly
	gen control = (`run' >= 0) if !mi(`run')

	//Restrict to bandwidth
	keep if abs(`run') < `bw'
	

	//Generate control function
	forvalues p=1/`deg' {
		gen poly`p' = `run'^`p'
	}


	//Run regression on micro data
	gen tmod = t+5
	reg `out' i.tmod##c.(poly* control c.control#c.poly*) , cluster(clust)


	clear
	set obs 5
	gen t 		= -5 + 2*(_n-1)
	gen tmod 	= 2*(_n-1)


	gen b 	= 0
	gen se 	= 0
	foreach tmod of numlist 2(2)8 {
		replace b 	= _b[`tmod'.tmod#c.control] 	if tmod == `tmod'
		replace se 	= _se[`tmod'.tmod#c.control] 	if tmod == `tmod'
	}

	local nclust = e(N_clust)


	#delimit;
	serrbar b se t, scale(`=invt(`nclust'-1, ${ci})') lp(dash dash) lc(blue blue)
						mvopt( mc(blue ) ms(S sh sh) )
						yline(0, lc(black) ) xline(0, lc(red) lp(dash) ) xline(2, lc(red) lp(dash) )
						legend(off) graphregion(color(white))
						xtitle("Years Until Redistricting", size(huge) )
						ytitle("`y`out''", size(huge) )
						title("`t`out''", size(huge) )
						xlabel(-5(2)3, labsize(vlarge) ) ylabel(-.6(.2).2, labsize(vlarge))
						text(.18 1 "First Election" "After" "Redistricting", color(red) )
						text(.18 -2 "<------------Before Redistricting", color(gs8) )
						text(.18 2.5 "After-->", color(gs8) ) name(`out')
						;
	#delimit cr

	restore
}

graph combine rep_share rep_win convert, rows(1) ysize(2) graphregion(color(white) fcolor(white))
graph drop rep_share rep_win convert


graph export "graphs/boundaryshift/conversion_rate.pdf", replace



/************
 Table 1
*************/

local out rep_win		//Outcome
local bw = 18			//Bandwidth
local binsize = 3		//Bin size
local deg = 1			//Polynomial degree
local run demhs2			//Running variable

use "dta/gerrymander/election_level_house.dta", clear

gen t=fed-year_red
replace t = t+1 if mod(t,2) == 0
replace t = t+10

preserve
//Sample: Elections in the specified year
keep if sample == 1 

$twocandset



//keep if fed >= 1972



//Cluster (for standard errors)
egen clust = group(year_redistrict state_id)

//Indicator for control of the assembly
gen control = (`run' >= 0) if !mi(`run')

//Restrict to bandwidth
keep if abs(`run') < `bw'

//Generate control function
forvalues p=1/`deg' {
	gen poly`p' = `run'^`p'
}


/*****
This is the constant and the trend
It turns out these really are not useful.
*****

//Run regression on micro data
eststo clear
eststo: reg `out' poly* control c.control#c.poly* , cluster(clust)
eststo: reg `out' c.t##c.(poly* control c.control#c.poly*) , cluster(clust)
reg `out' i.t##c.(poly*  c.control#c.poly*) ibn.t#c.control, cluster(clust) 
test 5.t#c.control = 7.t#c.control = 9.t#c.control
estadd scalar pval = r(p)
eststo

*/

/*
reg `out' i.fed i.clust poly* control c.control#c.poly* , cluster(clust)
estadd local cyclefe = "X"
estadd local yrfe = "X"
eststo



reg `out' i.fed i.clust c.t##c.(poly*  c.control#c.poly*) c.t#c.control, cluster(clust)
estadd local cyclefe = "X"
estadd local yrfe = "X"
eststo



reg `out' i.fed i.t##c.(poly*  c.control#c.poly*) ibn.t#c.control, cluster(clust) 
test 5.t#c.control = 7.t#c.control = 9.t#c.control
estadd local yrfe = "X"
estadd scalar pval = r(p)
eststo

tab t, gen(INDt)

reg `out' i.clust ib1.t##c.(poly* c.control#c.poly*) c.(INDt2-INDt5)#c.control, cluster(clust) 
test c.INDt3#c.control = c.INDt4#c.control = c.INDt5#c.control
estadd local cyclefe = "X"
estadd scalar pval = r(p)
eststo


reg `out' i.fed i.clust ib1.t##c.(poly* c.control#c.poly*) c.(INDt2-INDt5)#c.control, cluster(clust) 
test c.INDt3#c.control = c.INDt4#c.control = c.INDt5#c.control
estadd local cyclefe = "X"
estadd local yrfe = "X"
estadd scalar pval = r(p)
eststo
*/


eststo clear

tab t, gen(INDt)

reg `out' i.t##c.(poly*  c.control#c.poly*) c.(INDt1-INDt5)#c.control, cluster(clust) 
test c.INDt3#c.control = c.INDt4#c.control = c.INDt5#c.control
estadd scalar pval = r(p)
eststo

reg `out' i.fed i.t##c.(poly*  c.control#c.poly*) c.(INDt1-INDt5)#c.control, cluster(clust) 
test c.INDt3#c.control = c.INDt4#c.control = c.INDt5#c.control
estadd local yrfe = "X"
estadd scalar pval = r(p)
eststo

reg `out' i.clust ib1.t##c.(poly* c.control#c.poly*) c.(INDt2-INDt5)#c.control, cluster(clust) 
test c.INDt3#c.control = c.INDt4#c.control = c.INDt5#c.control
estadd local cyclefe = "X"
estadd scalar pval = r(p)
eststo


reg `out' i.fed i.clust ib1.t##c.(poly* c.control#c.poly*) c.(INDt2-INDt5)#c.control, cluster(clust) 
test c.INDt3#c.control = c.INDt4#c.control = c.INDt5#c.control
estadd local cyclefe = "X"
estadd local yrfe = "X"
estadd scalar pval = r(p)
eststo




esttab using "regressions/selection_small.tex", ///
replace b(3) se(3)  star(* 0.10 ** 0.05 *** 0.01) noobs sca("pval Test:  $\hat{\rd}_{-5}=\hat{\rd}_{-3}=\hat{\rd}_{-1}$" "cyclefe Event FEs" "yrfe Year FEs" "N  Observations" "N_clust Events" ) ///
rename(c.INDt1#c.control 1.t#c.control c.INDt2#c.control 3.t#c.control c.INDt3#c.control 5.t#c.control c.INDt4#c.control 7.t#c.control c.INDt5#c.control 9.t#c.control) ///
sfmt("%9.2g" "%9.0g")  nonotes keep(*.t#c.control )   mlab("Rep. Win" "Rep. Win" "Rep. Win" "Rep. Win" "Rep. Win" "Rep. Win")  ///
varlabel( 1.t#c.control "$\times \I(t=-9)$  " 3.t#c.control "$\times \I(t=-7)$" ///
5.t#c.control "$\times \I(t=-5)$" 7.t#c.control "$\times \I(t=-3)$" 9.t#c.control ///
"$\times \I(t=-1)$") refcat(1.t#c.control "[Democrats Control Assembly During Redistricting]", nolabel)

restore
/*******************
 Table 2 - Panel A
********************/

local out rep_win		//Outcome
local bw = 18			//Bandwidth
local deg = 1			//Polynomial degree
local run demhs2		//Running variable



/************
Prep
*************/

use "dta/gerrymander/election_level_house_qpanel.dta", clear




preserve
//Sample: Elections in the specified year
keep if sample == 1 

$twocandset



//keep if fed >= 1972


//Cluster (for standard errors)
egen clust = group(year_redistrict state_id)

//Indicator for control of the assembly
gen control = (`run' >= 0) if !mi(`run')

//Restrict to bandwidth
keep if abs(`run') < `bw'

//Generate control function
forvalues p=1/`deg' {
	gen poly`p' = `run'^`p'
}


//Election right after redistricting
gen postel = t == 1





/************
Specifications
*************/

//Controls
local sp1
local sp2 i.federal_election_year
local sp3 i.i
local sp4 i.i i.federal_election_year
local sp5 c.t##c.(poly* control c.control#c.poly*)
local sp6 i.i i.federal_election_year c.t##c.(poly* control c.control#c.poly*)

//Table markings
local mark1
local mark2 epfe
local mark3 yrfe
local mark4 epfe yrfe
local mark5 diftr
local mark6 epfe yrfe diftr

local mlabels ""

eststo clear
forvalues i=1/6 {
	reg `out' `sp`i'' c.postel##c.(poly* control c.control#c.poly*) , cluster(clust)
	
	foreach mark in `mark`i'' {
		estadd local `mark' = "X"
	}
	
	eststo
	
	local mlabels `"`mlabels' "Rep. Win" "'
}



esttab , b(3) se(3)  star(* 0.10 ** 0.05 *** 0.01) noobs sca("epfe Event FEs" "yrfe Year FEs" "diftr Trends" "N  Observations" "N_clust Events" ) ///
sfmt("%9.0g" "%9.0g" )  varlabel(c.postel#c.control "Dif-in-Disc Estimate") nonotes keep(c.postel#c.control) mlab(`mlabels')



esttab using "regressions/mainresult.tex", b(3) se(3) replace booktabs width(\textwidth) ///
	star(* 0.10 ** 0.05 *** 0.01) noobs sca( "epfe Event FEs" "yrfe Year FEs" "diftr Trends" "N  Observations" "N_clust Events") ///
	sfmt("%9.0g" "%9.0g" )  varlabel(c.postel#c.control "Dif-in-Disc Estimate") nonotes keep(c.postel#c.control) mlab(`mlabels')



/*****************
Table 2 - Panel B
******************/	

//Controls
//Conditions for regressions
local cond1 ""													//Baseline
local cond2 "drop if (hs_ind_elect > 0 & hs_ind_elect < .)"		//Drop elections with independent legislators
local cond3 "drop if vra == 1"									//Drop VRA Pre-Clearance States
local cond4 ""													//Republican control running variable (same sample as others)
local cond5 "drop if mod(federal_election_year, 2) == 1"		//No special elections

//Running Variables
local run1 demhs2
local run2 demhs2
local run3 demhs2
local run4 rephs2
local run5 demhs2
local run6 demhs2

//Outcomes
local out1 rep_win
local out2 rep_win
local out3 rep_win
local out4 rep_win
//local out5 dem_win
local out5 rep_win
local out6 rep_win

//Titles
local tit1 "Baseline"
local tit2 "No Ind. Assemblymen"
local tit3 "Drop VRA States"
local tit4 "Republican Margin"
//local tit5 "Outcome: Dem. Win"
local tit5 "Drop Special Elections"
local tit6 "Placebo"


local mlabels ""

eststo clear
forvalues i=1/6 {
	
	local out `out`i''		//Outcome
	local bw = 18			//Bandwidth
	local deg = 1			//Polynomial degree
	local run `run`i''		//Running variable



	/************
	Prep
	*************/
	
	if `i' == 6 {
		use "dta/gerrymander/placebo_election_level_house_qpanel.dta", clear
	}
	else {
		use "dta/gerrymander/election_level_house_qpanel.dta", clear
	}

	//Alternative running variable: fraction of Republicans
	gen rephs2		= 100 * ( hs_rep_elect- ceil(.5* hs_tot_elect) ) / hs_tot_elect

	//Indicator: VRA Pre-Clearance State
	gen vra = 0
	foreach state in "Arizona" "Texas" "Louisiana" "Mississippi" "Alabama"  "Georgia"  "South Carolina" "Alaska" "Virginia" {
		replace vra = 1 if statename == "`state'"
	}

	//Sample: Elections in the specified year
	keep if sample == 1 //& ${redistrictset}

	$twocandset



	//keep if fed >= 1972


	//Cluster (for standard errors)
	egen clust = group(year_redistrict state_id)

	//Indicator for control of the assembly
	gen control = (`run' >= 0) if !mi(`run')

	//Restrict to bandwidth
	keep if abs(`run') < `bw'

	//Generate control function
	forvalues p=1/`deg' {
		gen poly`p' = `run'^`p'
	}


	//Election right after redistricting
	gen postel = t == 1


	`cond`i''

	reg `out'  c.postel##c.(poly* control c.control#c.poly*) , cluster(clust)
	
	foreach mark in `mark`i'' {
		estadd local `mark' = "X"
	}
	
	eststo
	
	local mlabels `"`mlabels' "`tit`i''" "'
}



esttab , b(3) se(3)  star(* 0.10 ** 0.05 *** 0.01) noobs sca("N  Observations" "N_clust Events") ///
	sfmt("%9.0g" "%9.0g" )  varlabel(c.postel#c.control "  [Election After Red.]") nonotes keep(c.postel#c.control) mlab(`mlabels')


esttab using "regressions/robustness.tex", b(3) se(3) replace booktabs width(\textwidth) ///
	star(* 0.10 ** 0.05 *** 0.01) noobs sca("N  Observations" "N_clust Events") ///
	sfmt("%9.0g" "%9.0g" )  varlabel(c.postel#c.control "Dif-in-Disc Estimate") nonotes keep(c.postel#c.control) mlab(`mlabels')

	
	
/*******************
 Table 2 - Panel C
********************/	

local mlabels
eststo clear
foreach bw of numlist 22 18 14 10 6 {
	
	local out rep_win		//Outcome
	//local bw = 18			//Bandwidth
	local deg = 1			//Polynomial degree
	local run demhs2		//Running variable



	/************
	Prep
	*************/

	use "dta/gerrymander/election_level_house_qpanel.dta", clear

	
	//Sample: Elections in the specified year
	keep if sample == 1 //& ${redistrictset}

	$twocandset



	//keep if fed >= 1972


	//Cluster (for standard errors)
	egen clust = group(year_redistrict state_id)

	//Indicator for control of the assembly
	gen control = (`run' >= 0) if !mi(`run')

	//Restrict to bandwidth
	keep if abs(`run') < `bw'

	//Generate control function
	forvalues p=1/`deg' {
		gen poly`p' = `run'^`p'
	}


	//Election right after redistricting
	gen postel = t == 1




	reg `out' c.postel##c.(poly* control c.control#c.poly*) , cluster(clust)
	
	foreach mark in `mark`i'' {
		estadd local `mark' = "X"
	}
	
	eststo
	
	local mlabels `"`mlabels' "h=`bw'" "'
}



esttab , b(3) se(3)  star(* 0.10 ** 0.05 *** 0.01) noobs sca("N  Observations" "N_clust Events") ///
	sfmt("%9.0g" "%9.0g" )  varlabel(c.postel#c.control "Dif-in-Disc Estimate") nonotes keep(c.postel#c.control) mlab(`mlabels')


esttab using "regressions/bandwidth.tex", b(3) se(3) replace booktabs width(\textwidth) ///
	star(* 0.10 ** 0.05 *** 0.01) noobs sca("N  Observations" "N_clust Events") ///
	sfmt("%9.0g" "%9.0g" )  varlabel(c.postel#c.control "Dif-in-Disc Estimate") nonotes keep(c.postel#c.control) mlab(`mlabels')


	
/************
 Table 3
*************/

eststo clear
/************************************************************************
Do pre-gerrymandering districts differ in the fraction of minorities?
************************************************************************/

local run 	demhs2
local bw	18
local binsize = 3
local deg = 1
local out distprered_fracblack



use "dta/gerrymander/districtcomp_preselect.dta", clear

//Cluster (for standard errors)
egen clust = group(year_redistrict state_id)

//Indicator for control of the assembly
gen control = (`run' >= 0) if !mi(`run')

//Restrict to bandwidth
keep if abs(`run') < `bw'

keep if sample == 1

//Generate control function
forvalues p=1/`deg' {
	gen poly`p' = `run'^`p'
}


//Run regression on micro data
reg `out' poly* control c.control#c.poly* , cluster(clust)

sum `out' if !control & abs(`run') < 5 & e(sample)
estadd scalar conmean = r(mean)
eststo moreblack


/************************************************************************
Are African Americans more likely to have to be redistricted?
************************************************************************/
local run 	demhs2
local bw	18
local out distprered_absdev
local binsize = 3
local deg = 1


use "dta/gerrymander/districtcomp_needredistrict.dta", clear


keep if sample == 1

gen blackfrac	= b18ab/av0aa
gen blacktract 	=  blackfrac > .5 if !mi(blackfrac)
keep if blacktract == 1



//Cluster (for standard errors)
egen clust = group(year_redistrict state_id)

//Indicator for control of the assembly
gen control = (`run' >= 0) if !mi(`run')

//Restrict to bandwidth
keep if abs(`run') < `bw'

//Generate control function
forvalues p=1/`deg' {
	gen poly`p' = `run'^`p'
}


//Run regression on micro data
reg `out' poly* control c.control#c.poly* , cluster(clust)

sum `out' if !control & abs(`run') < 5 & e(sample)
estadd scalar conmean = r(mean)
eststo needred

restore

/************************************************************************
Do African-Americans Get Moved More Often?
************************************************************************/


local run 	demhs2
local bw	18
local samp 1
local out 

local binsize = 3
local deg = 1

local thresh .5

local titleblack "Majority Black Census Tracts"
local titleother "Other Census Tracts"





use "dta/gerrymander/frac_moved.dta", clear

egen clust = group(year_redistrict state_id)

//Regressions run on these will implicitly condition on whether the tract is
//majority black
gen frac_unfamiliar_black = frac_unfamiliar if (b18ab/av0aa > .5 & b18ab/ av0aa <= 1)
gen frac_unfamiliar_other = frac_unfamiliar if (b18ab/av0aa < .5 & b18ab/ av0aa <= 1)

gen moved_black			  = frac_unfamiliar_black > `thresh' if frac_unfamiliar_black < .
gen moved_other			  = frac_unfamiliar_other > `thresh' if frac_unfamiliar_other < .



//Pre-redistricting (placebo) or post?
keep if sample == 1 // & (year < 1990 | year > 1992) 


gen control = (`run' >= 0) if !mi(`run')
keep if abs(`run') < `bw'



forvalues p=1/`deg' {
	gen poly`p' = `run'^`p'
}


foreach type in black other {

	//local out frac_unfamiliar_`type'
	local out moved_`type'
	
	preserve

	reg `out' poly* control c.control#c.poly* `con', cluster(clust)
	
	sum `out' if !control & abs(`run') < 5 & e(sample)
	estadd scalar conmean = r(mean)
	eststo move`type'

	restore
}






/************************************************************************
Conditional on being moved, where are they moved?
************************************************************************/

/*********************
Overwhelmingly black?
**********************/
local run 	demhs2
local bw	10
local samp 1

local out new_district_majblack


local binsize = 1
local deg = 1

//Threshold for overwhelming
local thresh .75



/*
Prepare key variables
*/

use "dta/gerrymander/frac_moved.dta", clear

egen clust = group(year_redistrict state_id)

//Regressions run on these will implicitly condition on whether the tract is
//majority black
gen frac_unfamiliar_black 	= frac_unfamiliar if (b18ab/av0aa > .5 & b18ab/ av0aa <= 1)
gen frac_unfamiliar_other 	= frac_unfamiliar if (b18ab/av0aa < .5 & b18ab/ av0aa <= 1)

gen dist_fracblack = dist_b18ab/dist_av0aa

//Indicator for whether you are moved into a district with mostly black people
gen new_district_majblack 		= (dist_fracblack > `thresh') if !mi(frac_unfamiliar_black,dist_fracblack) & (frac_unfamiliar_black > .5) 





/*
Run test
*/

//Restrict to sample where legislature controls redistricting
keep if sample == 1 


gen control = (`run' >= 0) if !mi(`run')
keep if abs(`run') < `bw'



forvalues p=1/`deg' {
	gen poly`p' = `run'^`p'
}




reg `out' poly* control c.control#c.poly* `con', cluster(clust)

sum `out' if !control & abs(`run') < 2 & e(sample)
estadd scalar conmean = r(mean)
eststo packblack


/************************************************************************
Racial Gerrymandering Table
************************************************************************/

tempfile out1 out2
#delimit;
esttab moveblack moveother packblack moreblack needred using `out1', booktabs label            
		b(3) se(3)  star(* 0.10 ** 0.05 *** 0.01) noobs keep(control) nonotes
		mgroups("Prob. of Being Moved" "Conditional on Moving" "Pre-Redistricting Char." ,
		pattern(1 0 1 1 0 ) prefix(\multicolumn{@span}{c}{) suffix(})   
        span erepeat(\cmidrule(lr){@span})) 
		sca("N  Observations" "N_clust Events" ) sfmt("%9.0g" "%9.0g" "%9.2f")
		varlabel(control "Dem. Control") replace
		mtitle("Black Tracts" "Other Tracts" "New District $>$ 75\% Black" "Fraction Black" "District Size Deviation")  
		;
#delimit cr

filefilter `out1' `out2', from("\BStoprule") to("\BStoprule\BStoprule")
filefilter `out2' "regressions/racialgerrymandering.tex", from("\BSbottomrule") to("\BSbottomrule\BSbottomrule") replace



/************
 Table 4
*************/

/************************************************************************
Primary Data Set
************************************************************************/


use "dta/gerrymander/election_level_house_qpanel.dta", clear

keep if sample == 1 

${twocandset}

//Cluster (for standard errors)
egen clust 	= group(year_redistrict state_id)


reg rep_win demhs2 if !mi(demcon), cluster(clust)
keep if e(sample)


egen tag	= tag(clust)

//Share of seats won and chance of control
sum demhs2					if tag
local demhs2 				= string( r(mean), "%9.2f")
local demhs2sd				= string(r(sd), "%9.2f")

sum demcon					if tag
local demcon 				= string( r(mean), "%9.2f")

local keylist demhs2 demhs2sd demcon

foreach side in 1 0 {
	sum rep_win if demcon 	== `side'
	local repwin`side' 		= string(r(mean), "%9.2f")
	
	sum rep_share if demcon == `side'
	local repshare`side' 	= string(r(mean), "%9.2f")
	local repshare`side'sd 	= string(r(sd), "%9.2f")
	
	reg rep_win demhs2 if demcon == `side', cluster(clust)
	
	local nobs`side' 		= e(N)
	local nclust`side'		= e(N_clust)
	
	local keylist `keylist' repwin`side' repshare`side' repshare`side'sd nobs`side' nclust`side'

}



/************************************************************************
Open Elections
************************************************************************/
//We'll use the district panel for this, meaning only 1980 onwards
use "dta/gerrymander/district_panel_post.dta", clear
gen demcon = demhs2 >=0 if demhs2 < .
egen clust = group(year_redistrict state_id)

keep if open_consistent == 1

reg rep_win demhs2 if !mi(demcon), cluster(clust)
keep if e(sample)

foreach side in 1 0 {
	sum rep_win if demcon 			== `side'
	local repwin`side'open 			= string(r(mean), "%9.2f")
	
	sum rep_share if demcon 		== `side'
	local repshare`side'open 		= string(r(mean), "%9.2f")
	local repshare`side'sdopen 		= string(r(sd), "%9.2f")
	
	reg rep_win demhs2 if demcon 	== `side', cluster(clust)
	
	local nobs`side'open 			= e(N)
	local nclust`side'open			= e(N_clust)
	
	local keylist `keylist' repwin`side'open repshare`side'open repshare`side'sdopen nobs`side'open nclust`side'open


}




	
/************************************************************************
Output table using template
************************************************************************/
cap mkdir "tables"

//Base file:
tempfile desc0
filefilter "templates/descriptives_outcome.txt" "`desc0'", from(" ") to(" ")

//Loop
local onfile = 0
foreach key in `keylist' {
	local tmp = `onfile'+1
	tempfile desc`tmp'
	filefilter "`desc`onfile''" "`desc`tmp''", from("[`key']") to("``key''")
	local onfile = `tmp'
}

//The last temporary file we made is the final version
copy "`desc`tmp''" "tables/descriptives_outcome.tex", replace








/*******************************************************************************

					  Tables and Figures in the Appendices

*******************************************************************************/

/************
 Figure 8
*************/

local out rep_win		//Outcome
local bw = 18			//Bandwidth
local deg = 1			//Polynomial degree
local run demhs2		//Running variable

use "dta/gerrymander/election_level_house_qpanel.dta", clear


local set1 keep if year_red <= 1991
local set2 keep if year_red > 1991

local title1 "Redistricted 1971,1981,1991"
local title2 "Redistricted 2001, 2011"

foreach i in 1 2 {
	preserve
	//Sample: Elections in the specified year
	keep if sample == 1 

	$twocandset

	`set`i''

	//Cluster (for standard errors)
	egen clust = group(year_redistrict state_id)

	//Indicator for control of the assembly
	gen control = (`run' >= 0) if !mi(`run')

	//Restrict to bandwidth
	keep if abs(`run') < `bw'

	//Generate control function
	forvalues p=1/`deg' {
		gen poly`p' = `run'^`p'
	}


	//Run regression on micro data
	gen tmod = t+5
	reg `out' i.tmod##c.(poly* control c.control#c.poly*) , cluster(clust)


	clear
	set obs 5
	gen t 		= -5 + 2*(_n-1)
	gen tmod 	= 2*(_n-1)


	gen b 	= 0
	gen se 	= 0
	foreach tmod of numlist 2(2)8 {
		replace b 	= _b[`tmod'.tmod#c.control] 	if tmod == `tmod'
		replace se 	= _se[`tmod'.tmod#c.control] 	if tmod == `tmod'
	}

	local nclust = e(N_clust)



	#delimit;
	serrbar b se t, scale(`=invt(`nclust'-1, ${ci})') lp(dash dash) lc(blue blue)
						mvopt( mc(blue ) ms(S sh sh) )
						yline(0, lc(black) ) xline(0, lc(red) lp(dash) ) xline(2, lc(red) lp(dash) )
						legend(off) graphregion(color(white))
						xtitle("Years Until Redistricting", size(huge) )
						ytitle("Change in Probability Republican" "Wins when Democrats Take Control", size(huge) )
						xlabel(-5(2)3, labsize(huge) ) title("`title`i''", size(huge) )
						text(.18 1 "First Election" "After" "Redistricting", color(red) )
						text(.18 -2 "<------------Before Redistricting", color(gs8) )
						text(.18 2.5 "After-->", color(gs8) ) name(g`i') ylabel(-.4(.2).2, labsize(huge) )
						;
	#delimit cr

	restore

}


graph combine g1 g2, graphregion(color(white) fcolor(white)) xsize(8)
graph drop g1 g2


graph export "graphs/electionresults/recent.pdf", replace




/************
 Figure 9
*************/

global old_seed = c(seed)


/************************************************************************
Set parameters
*************************************************************************/
set seed 552293542
local threshold .6

local swing		.0665291
local spread	.0700259

local swingbias -.1

local shiftfrac = .1


/************************************************************************
Prepare dataset
*************************************************************************/

clear

set obs 25

gen id = _n

gen repshare = (rbeta(2,2))*.9  //*.8 +.1 //*.93
gen repshare_c = repshare



/************************************************************************
Gerrymander
*************************************************************************/
gen shareshift = `shiftfrac'*repshare

egen shifttot 	= total(shareshift)
local budget	= shifttot in 1
gsort -repshare
replace repshare = repshare - shareshift

sum id
local n = r(max)
forvalues i=1/`n' {
	local rcur = repshare in `i'
	
	if `rcur' < `threshold' {
		if `budget' > (`threshold' - `rcur') {
			local budget = `budget' - (`threshold' - `rcur' )
			replace repshare = `threshold' in `i'
		}
		else {
			replace repshare = repshare + `budget'  in `i'
			local budget 0
		}
	}
}


/************************************************************************
Outcome of First Election after Gerrymandering
*************************************************************************/

gen win 	= repshare > .5
gen win_c 	= repshare_c > .5

sum win
local win_ger =  string(100*r(mean), "%9.1f")


sum win_c
local win_c = string(100*r(mean), "%9.1f")


graph bar repshare_c repshare, over(id, sort(repshare_c) label(nolabels) ) yline(.5, lp(dash) lc(red)) ylabel(0(.25)1) legend(off) ///
			graphregion(color(white)) name(g1)  bar(1, color(black) ) bar(2, color(red) )  ///
			 ytitle("Republican Vote Share", size(huge) ) title("First Election", size(huge) ) ///
			text(.64 60 "Gerrymandered", color(red) size(huge) ) text(.8 83 "Natural", color(black)  size(huge) ) ///
			text(1 25 "-Seats won-", size(huge) ) text(.92 25.5 "Natural: ", size(huge) ) text(.85 17 "Gerrymandered:", size(huge) ) ///
			text(.92 41 "`win_c'%", size(huge) ) text(.85 41 "`win_ger'%", size(huge) )


/************************************************************************
Outcome of Later Election (after the swing towards Democrats kicks in)
*************************************************************************/

local aggtrend = `swing'*rnormal()				
gen trend =  `aggtrend' + `spread'*rnormal() + `swingbias'

gen next_repshare 	= min( max(repshare 		+ trend, 0) , 1)
gen next_repshare_c = min( max( repshare_c	+ trend, 0) , 1)

gen next_win 	= next_repshare > .5
gen next_win_c 	= next_repshare_c > .5


sum next_win
local win_ger =  string(100*r(mean), "%9.1f")


sum next_win_c
local win_c = string(100*r(mean), "%9.1f")

graph bar next_repshare_c next_repshare, over(id, sort(next_repshare_c)  label(nolabels) ) yline(.5, lp(dash) lc(red)) ylabel(0(.25)1) legend(off) ///
			graphregion(color(white)) name(g2) ytitle("Republican Vote Share", size(huge) ) title("Next Election", size(huge) ) ///
			bar(1, color(black) ) bar(2, color(red)  ) ///
			text(1 25 "-Seats won-", size(huge) ) text(.92 25.5 "Natural: ", size(huge) ) text(.85 17 "Gerrymandered:", size(huge) ) ///
			text(.92 41 "`win_c'%", size(huge) ) text(.85 41 "`win_ger'%", size(huge) )
			
graph combine g1 g2, graphregion( color(white) fcolor(white)) xsize(10)
graph drop g1 g2


cap mkdir graphs/sim
graph export "graphs/sim/example.pdf", replace


set seed ${old_seed}




/************
 Figure 10
*************/

use "simulations/swingsim2.dta", clear

foreach var of varlist win* next* {
	replace `var' = 100*`var'
}



gen dif1 = win- win_c
gen dif2 = next_win- next_win_c
gen dif3 = next2_win- next2_win_c


//twoway (kdensity dif1, bw(5)) (kdensity dif2, bw(5))

sum dif*, detail

foreach stat in p10 p50 p90 {
	foreach dif of varlist dif* {
		gen `stat'_`dif' = `dif'
	}
}

collapse win* next* dif* (p10) p10* (p50) p50* (p90) p90*, by(threshold shk)


gen int shkmod =  round(shk*100)
levelsof shkmod, local(list)

local labels2 text(47 .55 "Natural", color(black) size(huge) ) text(65 .6 "Gerrymandered", color(red) size(huge) ) 
local graphs
local num 0
scatter win_c win  threshold if shkmod == `num', connect(direct direct) name(g1) ylabel(0(25)100, labsize(huge)) xlabel(.5(1).8, labsize(huge)) ///
					graphregion(color(white)) legend(off) xtitle("Threshold", size(huge) )  mc(black red) lc(black red) ///
					ytitle("Percentage of Seats Won", size(huge) ) title("First Election", color(black) size(huge)) `labels2'
					
scatter next_win_c next_win  threshold  if shkmod == `num', connect(direct direct) name(g2) ylabel(0(25)100, labsize(huge) ) ///
					graphregion(color(white)) legend(off) xtitle("Threshold", size(huge) ) mc(black red) lc(black red) ///
					ytitle("Percentage of Seats Won", size(huge) ) title("Next Election", color(black)  size(huge))  xlabel(.5(1).8, labsize(huge))
					
	
graph combine g1 g2 , graphregion(color(white) fcolor(white)) xsize(12) rows(1) //name(s`count')
graph drop g1 g2 

graph export "graphs/sim/multisim_2.pdf", replace




/************
 Figure 11
*************/
local bw = 18			//Bandwidth
	local binsize = 3		//Bin size
	local deg = 1			//Polynomial degree
	local run demhs2			//Running variable
	local out rep_win
	
	use "dta/gerrymander/election_level_house.dta", clear
	
	local xtit1 ""
	local xtit2 ""
	local xtit3 " Seats Won by Democrats in {bf:State Assembly}"
	local xtit4 "During Redistricting Election (% of total, 0=50%)    "
	
	cap graph drop _all
	
	//Sample: Elections in the specified year
	keep if sample == 1 

		$twocandset
	
	//Restrict to bandwidth
	keep if abs(`run') < `bw'
	
	
	keep year_redistrict statename demhs2
	duplicates drop

	#d;
	hist demhs2, graphregion(color(white)) width(2.5) start(-20) freq
		xtitle(" Seats Won by Democrats in {bf:State Assembly}" "During Redistricting Election (% of total, 0=50%)", size(med))
		;
		#d cr
		
	graph export "graphs/appendix/rv_hist_freq.pdf", replace

	

/************
 Figure 12
*************/

local out rep_share		//Outcome
local bw = 18			//Bandwidth
local binsize = 4		//Bin size
local deg = 1			//Polynomial degree
local run demhs2			//Running variable

use "dta/gerrymander/election_level_house.dta", clear

egen totvotes = rowtotal(dem_votes rep_votes)
gen logvotes = log(totvotes)




local set1 "keep if fed < year_red - 10 + 5"
local set2 "keep if fed >= year_red - 10 + 5"

local title1 "6 to 10 Years Before Redistricting"
local title2 "1 to 5 Years Before Redistricting"

foreach s in 1 2 {
	preserve
	//Sample: Elections in the specified year
	keep if sample == 1 

	$twocandset
	
	

	//keep if fed >= 1972


	`set`s''
	
	collapse (sum) dem_votes rep_votes (first) `run', by(year_redistrict federal_election_year state_id)
	gen rep_share = rep_votes/(rep_votes+dem_votes)

	//Cluster (for standard errors)
	egen clust = group(year_redistrict state_id)

	//Indicator for control of the assembly
	gen control = (`run' >= 0) if !mi(`run')

	//Restrict to bandwidth
	keep if abs(`run') < `bw'

	//Generate control function
	forvalues p=1/`deg' {
		gen poly`p' = `run'^`p'
	}


	//Run regression on micro data
	reg `out' poly* control c.control#c.poly* , cluster(clust)

	//Size of discontinuity and standard error
	local b 	= string( _b[control], "%9.3f" )
	local se 	= string( _se[control], "%9.3f" )

	//Round to construct bins
	gen bin = `run' - mod(`run',`binsize')

	//Take bin averages (and counts)
	gen nonmi = e(sample)
	collapse `out' (sum)nobs=nonmi, by(bin)

	//Recenter the running variable on the midpoints of the bins
	gen `run' 					= bin+`binsize'/2
	gen control					= `run' > 0


	forvalues p=1/`deg' {
		gen poly`p' = `run'^`p'
	}
	predict pred

	#delimit;
	twoway 
	(scatteri 	.42 -15 "Republican Control"
					.42 1 "Democratic Control", ms(i) mlabsize(medsmall) mlabcolor(gs10) )
	(scatter `out' `run' ${useweight}, msize(2) mc(navy) )				//Dots
	(line pred `run' if control)  				//Fit: Republican control
	(line pred `run' if !control) 				//Fit: Democratic control
	,
	text(.58 -8 "Discontinuity" )  text(.58 0 "`b'" "(`se')" ) 
	graphregion(color(white)) legend(off) xline(0, lc(gs14) lp(dash) ) 
	xtitle(${runlabpre}, size(large) )
	ytitle("Rep. Vote Share" , size(vlarge)) ylabel(.4 (.1) .6)
	title("`title`s''", size(vlarge) ) name(pre`s') xline(0, lc(gs14) lp(dash) )
	;
	#delimit cr

	restore

}







/************************************************************************
Post-Trend
************************************************************************/


use "dta/gerrymander/election_level_house_post.dta", clear

egen totvotes = rowtotal(dem_votes rep_votes)
gen logvotes = log(totvotes)






local set1 "keep if fed == year_red + 1"
local set2 "keep if fed >= year_red + 7"

local title1 "1 Year After Redistricting"
local title2 "7 to 9 Years After Redistricting"

foreach s in 1 2 {
	preserve
	//Sample: Elections in the specified year
	keep if sample == 1 

	$twocandset
	
	

	//keep if fed >= 1972


	`set`s''
	
	collapse (sum) dem_votes rep_votes (first) `run', by(year_redistrict federal_election_year state_id)
	gen rep_share = rep_votes/(rep_votes+dem_votes)


	//Cluster (for standard errors)
	egen clust = group(year_redistrict state_id)

	//Indicator for control of the assembly
	gen control = (`run' >= 0) if !mi(`run')

	//Restrict to bandwidth
	keep if abs(`run') < `bw'

	//Generate control function
	forvalues p=1/`deg' {
		gen poly`p' = `run'^`p'
	}


	//Run regression on micro data
	reg `out' poly* control c.control#c.poly* , cluster(clust)

	//Size of discontinuity and standard error
	local b 	= string( _b[control], "%9.3f" )
	local se 	= string( _se[control], "%9.3f" )

	//Round to construct bins
	gen bin = `run' - mod(`run',`binsize')

	//Take bin averages (and counts)
	gen nonmi = e(sample)
	collapse `out' (sum)nobs=nonmi, by(bin)

	//Recenter the running variable on the midpoints of the bins
	gen `run' 					= bin+`binsize'/2
	gen control					= `run' > 0


	forvalues p=1/`deg' {
		gen poly`p' = `run'^`p'
	}
	predict pred

	#delimit;
	twoway 
	(scatteri 	.42 -15 "Republican Control"
					.42 1 "Democratic Control", ms(i) mlabsize(medsmall) mlabcolor(gs10) )
	(scatter `out' `run' ${useweight}, msize(2) mc(navy) )				//Dots
	(line pred `run' if control)  				//Fit: Republican control
	(line pred `run' if !control) 				//Fit: Democratic control
	,
	text(.58 -8 "Discontinuity")  text(.58 0 "`b'" "(`se')" ) 
	graphregion(color(white)) legend(off) xline(0, lc(gs14) lp(dash) ) 
	xtitle(${runlabpost}, size(large) )
	ytitle("Rep. Vote Share" , size(vlarge)) ylabel(.4 (.1) .6)
	title("`title`s''", size(vlarge) ) name(post`s') xline(0, lc(gs14) lp(dash) )
	;
	#delimit cr

	restore

}


graph combine pre1 pre2 post1 post2, graphregion(color(white) fcolor(white) ) rows(2) xsize(7)
graph drop pre1 pre2 post1 post2

graph export "graphs/appendix/repshare_state.pdf", replace



/************
 Figure 13
*************/

local run 	demhs2
local bw	18
local samp 1
local out 

local binsize = 3
local deg = 1

local thresh .5

local titleblack "Majority Black Census Tracts"
local titleother "Other Census Tracts"





use "dta/gerrymander/frac_moved.dta", clear

egen clust = group(year_redistrict state_id)

//Regressions run on these will implicitly condition on whether the tract is
//majority black
gen frac_unfamiliar_black = frac_unfamiliar if (b18ab/av0aa > .5 & b18ab/ av0aa <= 1)
gen frac_unfamiliar_other = frac_unfamiliar if (b18ab/av0aa < .5 & b18ab/ av0aa <= 1)

gen moved_black			  = frac_unfamiliar_black > `thresh' if frac_unfamiliar_black < .
gen moved_other			  = frac_unfamiliar_other > `thresh' if frac_unfamiliar_other < .



//Pre-redistricting (placebo) or post?
keep if sample == 1 // & (year < 1990 | year > 1992) 


gen control = (`run' >= 0) if !mi(`run')
keep if abs(`run') < `bw'



forvalues p=1/`deg' {
	gen poly`p' = `run'^`p'
}


foreach type in black other {

	//local out frac_unfamiliar_`type'
	local out moved_`type'
	
	preserve

	reg `out' poly* control c.control#c.poly* `con', cluster(clust)
	
	sum `out' if !control & abs(`run') < 5 & e(sample)
	estadd scalar conmean = r(mean)
	eststo move`type'

	//Size of discontinuity and standard error
	local b 	= string( _b[control], "%9.3f" )
	local se 	= string( _se[control], "%9.3f" )


	gen bin = `run' - mod(`run',`binsize')

	//Take bin averages (and counts)
	gen nonmi = e(sample)

	//Reweight
	collapse `out', by(bin)


	gen `run' 					= bin+`binsize'/2
	gen control					= (`run' >= 0) if !mi(`run')


	forvalues p=1/`deg' {
		gen poly`p' = `run'^`p'
	}
	predict pred


	/*
	#delimit;
	twoway 
	(scatteri 	.21 -10 "Republican Control"
				.21 0 "Democratic Control", ms(i) mlabsize(large) mlabcolor(gs10) )
	(scatter `out' `run' ${useweight}, msize(3) mc(navy) )				//Dots
	(line pred `run' if control)  				//Fit: Republican control
	(line pred `run' if !control) 				//Fit: Democratic control
	,

	text(.25 6 "Discontinuity", size(vlarge) )  text(.25 12 "`b'" "(`se')", size(vlarge) ) 
	graphregion(color(white)) legend(off) xline(0, lc(gs14) lp(dash) ) 
	xtitle("Margin of Seats Won by Democrats (%)") xlabel(-18(6)18)
	ytitle("Fraction of New District that is Unfamiliar", size(large) ) ylabel(.2(.1).5)
	title("`title`type''") name(`type')
	;
	#delimit cr
	*/
	
	#delimit;
	twoway 
	(scatteri 	.01 -10 "Republican Control"
				.01 0 "Democratic Control", ms(i) mlabsize(large) mlabcolor(gs10) )
	(scatter `out' `run' ${useweight}, msize(3) mc(navy) )				//Dots
	(line pred `run' if control)  				//Fit: Republican control
	(line pred `run' if !control) 				//Fit: Democratic control
	,

	text(.1 6 "Discontinuity:", size(vlarge) )  text(.1 12 "`b'" "(`se')", size(vlarge) ) 
	graphregion(color(white)) legend(off) xline(0, lc(gs14) lp(dash) ) 
	xtitle(${runlabdem}, size(huge) ) xlabel(-18(6)18)
	ytitle("Probability of Being Moved", size(huge) ) ylabel(0(.1).5)
	title("`title`type''", size(huge) ) name(`type')
	;
	#delimit cr
	
	restore
}


graph combine black other, graphregion(color(white) fcolor(white)) ysize(2)
graph drop black other


cap mkdir graphs/boundaryshift
graph export "graphs/boundaryshift/unfamiliar.pdf", replace



/************
 Figure 14
*************/

/*********************
Overwhelmingly black?
**********************/
local run 	demhs2
local bw	10
local samp 1

local out new_district_majblack


local binsize = 1
local deg = 1

//Threshold for overwhelming
local thresh .75



/*
Prepare key variables
*/

use "dta/gerrymander/frac_moved.dta", clear

egen clust = group(year_redistrict state_id)

//Regressions run on these will implicitly condition on whether the tract is
//majority black
gen frac_unfamiliar_black 	= frac_unfamiliar if (b18ab/av0aa > .5 & b18ab/ av0aa <= 1)
gen frac_unfamiliar_other 	= frac_unfamiliar if (b18ab/av0aa < .5 & b18ab/ av0aa <= 1)

gen dist_fracblack = dist_b18ab/dist_av0aa

//Indicator for whether you are moved into a district with mostly black people
gen new_district_majblack 		= (dist_fracblack > `thresh') if !mi(frac_unfamiliar_black,dist_fracblack) & (frac_unfamiliar_black > .5) 





/*
Run test
*/

//Restrict to sample where legislature controls redistricting
keep if sample == 1 


gen control = (`run' >= 0) if !mi(`run')
keep if abs(`run') < `bw'



forvalues p=1/`deg' {
	gen poly`p' = `run'^`p'
}




reg `out' poly* control c.control#c.poly* `con', cluster(clust)

sum `out' if !control & abs(`run') < 2 & e(sample)
estadd scalar conmean = r(mean)
eststo packblack

//Size of discontinuity and standard error
local b 	= string( _b[control], "%9.3f" )
local se 	= string( _se[control], "%9.3f" )





/*
Construct figure
*/

gen bin = `run' - mod(`run',`binsize')

//Take bin averages (and counts)
gen nonmi = e(sample)

//Reweight
collapse `out', by(bin)


gen `run' 					= bin+`binsize'/2
gen control					= (`run' >= 0) if !mi(`run')


forvalues p=1/`deg' {
	gen poly`p' = `run'^`p'
}
predict pred



#delimit;
twoway 
(scatteri 	-.05 -6.5 "Republican Control"
				-.05 0 "Democratic Control", ms(i) mlabsize(medlarge) mlabcolor(gs10) )
(scatter `out' `run' ${useweight}, msize(3) mc(navy) )				//Dots
(line pred `run' if control)  				//Fit: Republican control
(line pred `run' if !control) 				//Fit: Democratic control
,
xline(0, lc(gs14) lp(dash) ) yline(0, lc(black) )
text(.25 3 "Discontinuity:", size(large) )  text(.25 8 "`b'" "(`se')", size(large) ) 
graphregion(color(white)) legend(off)  xlabel(-10(5)10)
xtitle(${runlabdem}, size(medlarge))
ytitle("Probability of Being Moved" "to a District > 75% Black", size(medlarge)) ylabel(-.05(.05).35)
title("Moved to Overwhelmingly Black District?", size(large) ) // name(overwhelming)
;
#delimit cr

graph export "graphs/boundaryshift/overwhelming.pdf", replace



/************
 Figure 15
*************/
eststo clear
/************************************************************************
Do pre-gerrymandering districts differ in the fraction of minorities?
************************************************************************/


local run 	demhs2
local bw	18
local binsize = 3
local deg = 1
local out distprered_fracblack



use "dta/gerrymander/districtcomp_preselect.dta", clear

//Cluster (for standard errors)
egen clust = group(year_redistrict state_id)

//Indicator for control of the assembly
gen control = (`run' >= 0) if !mi(`run')

//Restrict to bandwidth
keep if abs(`run') < `bw'

keep if sample == 1

//Generate control function
forvalues p=1/`deg' {
	gen poly`p' = `run'^`p'
}


//Run regression on micro data
reg `out' poly* control c.control#c.poly* , cluster(clust)

sum `out' if !control & abs(`run') < 5 & e(sample)
estadd scalar conmean = r(mean)
eststo moreblack

//Size of discontinuity and standard error
local b 	= string( _b[control], "%9.3f" )
local se 	= string( _se[control], "%9.3f" )

//Round to construct bins
gen bin = `run' - mod(`run',`binsize')

//Take bin averages (and counts)
gen nonmi = e(sample)
collapse `out' (sum)nobs=nonmi, by(bin)

//Recenter the running variable on the midpoints of the bins
gen `run' 					= bin+`binsize'/2
gen control					= `run' > 0


forvalues p=1/`deg' {
	gen poly`p' = `run'^`p'
}
predict pred

#delimit;
twoway 
(scatteri 	.01 -10 "Republican Control"
				.01 1 "Democratic Control", ms(i) mlabsize(medlarge) mlabcolor(gs10) )
(scatter `out' `run' ${useweight}, msize(3) mc(navy) )				//Dots
(line pred `run' if control)  				//Fit: Republican control
(line pred `run' if !control) 				//Fit: Democratic control
,
text(.05 -15 "Discontinuity:", size(huge) )  text(.05 -7 "`b'" "(`se')", size(huge) ) 
graphregion(color(white)) legend(off) xline(0, lc(gs14) lp(dash) ) 
xtitle(${runlabdem}, size(huge) )
ytitle("Fraction of District Population", size(huge) )  ylabel(0(.05).15)
 name(level) title("Fraction of Population Black" "is Similar at Threshold", size(huge) )
;
#delimit cr




/************************************************************************
Are African Americans more likely to have to be redistricted?
************************************************************************/
local run 	demhs2
local bw	18
local out distprered_absdev
local binsize = 3
local deg = 1


use "dta/gerrymander/districtcomp_needredistrict.dta", clear


keep if sample == 1

gen blackfrac	= b18ab/av0aa
gen blacktract 	=  blackfrac > .5 if !mi(blackfrac)
keep if blacktract == 1



//Cluster (for standard errors)
egen clust = group(year_redistrict state_id)

//Indicator for control of the assembly
gen control = (`run' >= 0) if !mi(`run')

//Restrict to bandwidth
keep if abs(`run') < `bw'

//Generate control function
forvalues p=1/`deg' {
	gen poly`p' = `run'^`p'
}


//Run regression on micro data
reg `out' poly* control c.control#c.poly* , cluster(clust)

sum `out' if !control & abs(`run') < 5 & e(sample)
estadd scalar conmean = r(mean)
eststo needred


//Size of discontinuity and standard error
local b 	= string( _b[control], "%9.3f" )
local se 	= string( _se[control], "%9.3f" )

//Round to construct bins
gen bin = `run' - mod(`run',`binsize')

//Take bin averages (and counts)
gen nonmi = e(sample)
collapse `out' (sum)nobs=nonmi, by(bin)

//Recenter the running variable on the midpoints of the bins
gen `run' 					= bin+`binsize'/2
gen control					= `run' > 0


forvalues p=1/`deg' {
	gen poly`p' = `run'^`p'
}
predict pred

#delimit;
twoway 
(scatteri 	.01 -10 "Republican Control"
				.01 1 "Democratic Control", ms(i) mlabsize(medlarge) mlabcolor(gs10) )
(scatter `out' `run' ${useweight}, msize(3) mc(navy) )				//Dots
(line pred `run' if control)  				//Fit: Republican control
(line pred `run' if !control) 				//Fit: Democratic control
,
text(.05 -15 "Discontinuity:", size(huge) )  text(.05 -7 "`b'" "(`se')", size(huge) ) 
graphregion(color(white)) legend(off) name(needred) xline(0, lc(gs14) lp(dash) )
xtitle(${runlabdem}, size(huge) )
ytitle("Deviation in District Population", size(huge) )  ylabel(0(.05).15)
  title("Blacks Are No More Likely" "to Live in Large or Small Districts", size(huge) )
;
#delimit cr

graph combine level needred, graphregion( color(white) fcolor(white) ) rows(1) ysize(2)


graph export "graphs/boundaryshift/nodiscblack.pdf", replace




/************
 Figure 16
*************/


/*********************
What density?
**********************/
local run 	demhs2
local bw	10
local denbw .1



tempfile rdest
cap postclose rdest
postfile rdest den b se nclust using `rdest'

use "dta/gerrymander/frac_moved.dta", clear


//Packing black citizens
gen dist_fracblack = dist_b18ab/ dist_av0aa

//Regressions run on these will implicitly condition on whether the tract is
//majority black
gen frac_unfamiliar_black 	= frac_unfamiliar if (b18ab/av0aa > .5 & b18ab/ av0aa <= 1)


egen clust = group(year_redistrict state_id)

gen control = (`run' >= 0) if !mi(`run')
keep if abs(`run') < `bw'

keep if sample == 1

drop if anyindependents == 1

egen tag = tag(id)

//Population fraction of blacks
sum dist_fracblack [aw=tractpop] //if tag
local popfrac = r(mean)


foreach den of numlist .01(.025).99 {
	
	//Density
	cap drop black_den
	gen black_den		= ( abs(dist_fracblack - `den') < `denbw' ) if !mi(frac_unfamiliar_black,dist_fracblack) & (frac_unfamiliar_black > .5) 
	



	reg black_den demhs2 c.control#c.demhs2  control `con', cluster(clust)
	//reg black_den  control `con' , cluster(clust)
	
	
	local b 		= _b[control]
	local se		= _se[control]
	local nclust	= e(N_clust)
	
	post rdest (`den') (`b') (`se') (`nclust')
	
	
}


postclose rdest

use `rdest', clear

replace b = -b
gen bl = b - invt(nclust-1, ${ci})*se
gen bu = b + invt(nclust-1, ${ci})*se

local estcol gs8
#delimit;
scatter b bl bu den, sort(den) connect(direct direct direct) lp(solid dash dash) 
					lc(`estcol' `estcol' `estcol') mc(`estcol' `estcol' `estcol') 
					ms(o p p) 
					ylabel(-.75(.25).75) yline(0, lc(black) ) 
					graphregion(color(white)) xtitle("Black Population Share of New District", size(vlarge)) legend(off)
					ytitle("Estimated Shift in Density (${cilabel}% CI) ", size(vlarge) )
					xline(.5, lc(red) lp(longdash) )  name(density)
		;
#delimit cr

graph export "graphs/boundaryshift/gerrydens.pdf", replace


//graphs/background/density_small.pdf <-- pdf created from ppt




/************
 Figure 17
*************/

local out rep_win		//Outcome
local binsize = 3		//Bin size
local deg = 1			//Polynomial degree
local run demhs2			//Running variable

use "dta/gerrymander/election_level_house.dta", clear

gen t=fed-year_red
replace t = t+1 if mod(t,2) == 0
replace t = t+10

local coef bw nclust
foreach n of numlist 2/5 {
	local coef `coef' b`n' se`n'
}

tempfile estimates

cap postclose estimates
postfile estimates `coef' using `estimates'

forvalues bw=8(.5)22 {

	preserve
	//Sample: Elections in the specified year
	keep if sample == 1 

	$twocandset

	//Cluster (for standard errors)
	egen clust = group(year_redistrict state_id)

	//Indicator for control of the assembly
	gen control = (`run' >= 0) if !mi(`run')




	//Restrict to bandwidth
	keep if abs(`run') < `bw'

	//Generate control function
	forvalues p=1/`deg' {
		gen poly`p' = `run'^`p'
	}



	tab t, gen(INDt)

	reg `out' i.fed i.clust ib1.t##c.(poly* c.control#c.poly*) c.(INDt2-INDt5)#c.control, cluster(clust) 
	
	local nclust = e(N_clust)
	local postcom (`bw') (`nclust')
	foreach n of numlist 2/5 {
		local tmp1 = _b[c.INDt`n'#c.control]
		local tmp2 = _se[c.INDt`n'#c.control]
		local postcom `postcom' ( `tmp1' ) ( `tmp2' )
	}

	post estimates `postcom'

	restore
}

postclose estimates

use `estimates', clear



foreach n of numlist 2/5 {
	gen b`n'l = b`n' - invt(nclust-1, ${ci})*se`n'
	gen b`n'u = b`n' + invt(nclust-1, ${ci})*se`n'
}

foreach n of numlist 2/5 {
	local ndisp = 9-(`n'-1)*2
	local estcol gs8
	#delimit;
	scatter b`n' b`n'l b`n'u bw, sort(bw) connect(direct direct direct) lp(solid dash dash) 
						lc(`estcol' `estcol' `estcol') mc(`estcol' `estcol' `estcol') 
						ms(o p p)  xlabel(8(4)22, labsize(large) ) title("Selection Effect at t=-`ndisp'", size(large) )
						yline(0, lc(black) ) ylabe(-.2(.2).6) 
						graphregion(color(white)) xtitle("Bandwidth", size(large) ) legend(off)
						ytitle("RD Estimate (${cilabel}% CI) ", size(large) ) name(b`n')
						
			;
	#delimit cr
}



graph combine b2 b3 b4 b5, graphregion(color(white) fcolor(white) ) rows(2) xsize(6)
graph drop b2 b3 b4 b5

graph export "graphs/appendix/selection_bw.pdf", replace




/************
 Figure 18
*************/

local titleblack "Majority Black Census Tracts"
local titleother "Other Census Tracts"

foreach race in black other {
	
	

	local run 	demhs2
	local deg = 1
	local out moved_`race'
	local thresh .5
	
	use "dta/gerrymander/frac_moved.dta", clear

	egen clust = group(year_redistrict state_id)

	//Regressions run on these will implicitly condition on whether the tract is
	//majority black
	gen frac_unfamiliar_black = frac_unfamiliar if (b18ab/av0aa > .5 & b18ab/ av0aa <= 1)
	gen frac_unfamiliar_other = frac_unfamiliar if (b18ab/av0aa < .5 & b18ab/ av0aa <= 1)

	gen moved_black			  = frac_unfamiliar_black > `thresh' if frac_unfamiliar_black < .
	gen moved_other			  = frac_unfamiliar_other > `thresh' if frac_unfamiliar_other < .



	//Pre-redistricting (placebo) or post?
	keep if sample == 1 // & (year < 1990 | year > 1992) 



	cap postclose rdest
	tempfile rdest
	postfile rdest bw b se nclust using `rdest'

	foreach bw of numlist 8(.5)22 {
		preserve

		gen control = (`run' >= 0) if !mi(`run')
		keep if abs(`run') < `bw'



		forvalues p=1/`deg' {
			gen poly`p' = `run'^`p'
		}
		reg `out' poly* control c.control#c.poly* `con', cluster(clust)


		local b 		= _b[control]
		local se		= _se[control]
		local nclust	= e(N_clust)
		
		post rdest (`bw') (`b') (`se') (`nclust')
		
		restore
	}



	postclose rdest

	use `rdest', clear

	gen bl = b - invt(nclust-1, ${ci})*se
	gen bu = b + invt(nclust-1, ${ci})*se

	local estcol gs8
	#delimit;
	scatter b bl bu bw, sort(bw) connect(direct direct direct) lp(solid dash dash) 
						lc(`estcol' `estcol' `estcol') mc(`estcol' `estcol' `estcol') 
						ms(o p p)  xlabel(8(4)22) title("`title`race''", size(huge))
						yline(0, lc(black) ) ylabel(-.4(.1).1) xlabel(, labsize(huge))
						graphregion(color(white)) xtitle("Bandwidth", size(huge)) legend(off)
						ytitle("RD Estimate (${cilabel}% CI) ", size(large) ) name(`race')
						
			;
	#delimit cr
}

graph combine black other, graphregion(color(white) fcolor(white) ) rows(2)
graph drop black other


graph export "graphs/appendix/unfamiliar_bw.pdf", replace


/************
 Figure 19
*************/
/************************************************************************
Conditional on Being Moved, what kind of district are they moved to?
************************************************************************/

/*********************
Overwhelmingly black?
**********************/

local run 	demhs2
local samp 1
local deg = 1
local out new_district_majblack


//Threshold for overwhelming
local thresh .75

/*
Prepare key variables
*/

use "dta/gerrymander/frac_moved.dta", clear

egen clust = group(year_redistrict state_id)

//Regressions run on these will implicitly condition on whether the tract is
//majority black
gen frac_unfamiliar_black 	= frac_unfamiliar if (b18ab/av0aa > .5 & b18ab/ av0aa <= 1)
gen frac_unfamiliar_other 	= frac_unfamiliar if (b18ab/av0aa < .5 & b18ab/ av0aa <= 1)

gen dist_fracblack = dist_b18ab/dist_av0aa

//Indicator for whether you are moved into a district with mostly black people
gen new_district_majblack 		= (dist_fracblack > `thresh') if !mi(frac_unfamiliar_black,dist_fracblack) & (frac_unfamiliar_black > .5) 


//Restrict to sample where legislature controls redistricting
keep if sample == 1 



cap postclose rdest
tempfile rdest
postfile rdest bw b se nclust using `rdest'

foreach bw of numlist 4(.5)12 {

	preserve
	
	gen control = (`run' >= 0) if !mi(`run')
	keep if abs(`run') < `bw'



	forvalues p=1/`deg' {
		gen poly`p' = `run'^`p'
	}



	reg `out' poly* control c.control#c.poly* `con', cluster(clust)



	local b 		= _b[control]
	local se		= _se[control]
	local nclust	= e(N_clust)
	
	post rdest (`bw') (`b') (`se') (`nclust')
	
	restore
}





postclose rdest

use `rdest', clear

gen bl = b - invt(nclust-1, ${ci})*se
gen bu = b + invt(nclust-1, ${ci})*se

local estcol gs8
#delimit;
scatter b bl bu bw, sort(bw) connect(direct direct direct) lp(solid dash dash) 
					lc(`estcol' `estcol' `estcol') mc(`estcol' `estcol' `estcol') 
					ms(o p p)  xlabel(4(2)12) ylabel(-.5(.1)0)
					yline(0, lc(black) )
					graphregion(color(white)) xtitle("Bandwidth") legend(off)
					ytitle("RD Estimate (${cilabel}% CI) ") yline(0, lc(black) )
					
		;
#delimit cr



graph export "graphs/appendix/packblack_bw.pdf", replace

/************
 Figure 20
*************/

foreach bw of numlist 6 8 10 {
	local run 	demhs2
	local denbw .1



	tempfile rdest
	cap postclose rdest
	postfile rdest den b se nclust using `rdest'

	use "dta/gerrymander/frac_moved.dta", clear


	//Packing black citizens
	gen dist_fracblack = dist_b18ab/ dist_av0aa

	//Regressions run on these will implicitly condition on whether the tract is
	//majority black
	gen frac_unfamiliar_black 	= frac_unfamiliar if (b18ab/av0aa > .5 & b18ab/ av0aa <= 1)


	egen clust = group(year_redistrict state_id)

	gen control = (`run' >= 0) if !mi(`run')
	keep if abs(`run') < `bw'

	keep if sample == 1

	drop if anyindependents == 1

	egen tag = tag(id)

	//Population fraction of blacks
	sum dist_fracblack [aw=tractpop] //if tag
	local popfrac = r(mean)


	foreach den of numlist .01(.025).99 {
		

		//Density
		cap drop black_den
		gen black_den		= ( abs(dist_fracblack - `den') < `denbw' ) if !mi(frac_unfamiliar_black,dist_fracblack) & (frac_unfamiliar_black > .5) 
		



		reg black_den demhs2 c.control#c.demhs2  control `con', cluster(clust)
		//reg black_den  control `con' , cluster(clust)
		
		
		local b 		= _b[control]
		local se		= _se[control]
		local nclust	= e(N_clust)
		
		post rdest (`den') (`b') (`se') (`nclust')
		
		
	}



	postclose rdest

	use `rdest', clear

	replace b = -b
	gen bl = b - invt(nclust-1, ${ci})*se
	gen bu = b + invt(nclust-1, ${ci})*se

	local estcol gs8
	#delimit;
	scatter b bl bu den, sort(den) connect(direct direct direct) lp(solid dash dash) 
						lc(`estcol' `estcol' `estcol') mc(`estcol' `estcol' `estcol') 
						ms(o p p) title("Density of Black Population Share [Bw=`bw']")
						ylabel(-.75(.25).75) yline(0, lc(black) ) 
						graphregion(color(white)) xtitle("People living in districts with ____ fraction black") legend(off)
						ytitle("Estimated Shift in Density (${cilabel}% CI) " )
						xline(.5, lc(red) lp(longdash) )  name(g`bw')
			;
	#delimit cr

}


graph combine g10 g8 g6, graphregion( color(white) fcolor(white) ) rows(3) ysize(8)
graph drop g10 g8 g6


graph export "graphs/appendix/blackden_bw.pdf", replace



/************
 Figure 21
*************/

/************
Do pre-gerrymandering districts differ in the fraction of minorities?
*************/

local run 	demhs2
local out distprered_fracblack
local deg = 1


use "dta/gerrymander/districtcomp_preselect.dta", clear


tempfile rdest
cap postclose rdest
postfile rdest bw b se nclust using `rdest'


foreach bw of numlist 4(.5)22 {
	preserve

	//Cluster (for standard errors)
	egen clust = group(year_redistrict state_id)

	//Indicator for control of the assembly
	gen control = (`run' >= 0) if !mi(`run')

	//Restrict to bandwidth
	keep if abs(`run') < `bw'

	keep if sample == 1

	//Generate control function
	forvalues p=1/`deg' {
		gen poly`p' = `run'^`p'
	}


	//Run regression on micro data
	reg `out' poly* control c.control#c.poly* , cluster(clust)

	

	local b 		= _b[control]
	local se		= _se[control]
	local nclust	= e(N_clust)
	
	post rdest (`bw') (`b') (`se') (`nclust')

	restore
}

postclose rdest




	
	
use `rdest', clear

gen bl = b - invt(nclust-1, ${ci})*se
gen bu = b + invt(nclust-1, ${ci})*se

local estcol gs8
#delimit;
scatter b bl bu bw, sort(bw) connect(direct direct direct) lp(solid dash dash) 
					lc(`estcol' `estcol' `estcol') mc(`estcol' `estcol' `estcol') 
					ms(o p p)  xlabel(4(6)22, labsize(large) ) title("Fraction of Population Black is Similar at Threshold", size(large) )
					yline(0, lc(black) ) ylabe(-.15(.075).15) 
					graphregion(color(white)) xtitle("Bandwidth", size(large) ) legend(off)
					ytitle("RD Estimate (${cilabel}% CI) ", size(large) ) name(lev)
					
		;
#delimit cr




/***********
Are African Americans more likely to have to be redistricted?
************/
use "dta/gerrymander/districtcomp_needredistrict.dta", clear


keep if sample == 1


gen blackfrac	= b18ab/av0aa
gen blacktract 	=  blackfrac > .5 if !mi(blackfrac)
keep if blacktract == 1



tempfile rdest
cap postclose rdest
postfile rdest bw b se nclust using `rdest'


local run 	demhs2
local out distprered_absdev
local deg = 1

foreach bw of numlist 4(.5)22 {
	preserve

	keep if blacktract == 1

	//Cluster (for standard errors)
	egen clust = group(year_redistrict state_id)

	//Indicator for control of the assembly
	gen control = (`run' >= 0) if !mi(`run')

	//Restrict to bandwidth
	keep if abs(`run') < `bw'

	//Generate control function
	forvalues p=1/`deg' {
		gen poly`p' = `run'^`p'
	}


	//Run regression on micro data
	reg `out' poly* control c.control#c.poly* , cluster(clust)

	

	local b 		= _b[control]
	local se		= _se[control]
	local nclust	= e(N_clust)
	
	post rdest (`bw') (`b') (`se') (`nclust')

	restore
}

postclose rdest


use `rdest', clear

gen bl = b - invt(nclust-1, ${ci})*se
gen bu = b + invt(nclust-1, ${ci})*se

local estcol gs8
#delimit;
scatter b bl bu bw, sort(bw) connect(direct direct direct) lp(solid dash dash) 
					lc(`estcol' `estcol' `estcol') mc(`estcol' `estcol' `estcol') 
					ms(o p p)  xlabel(4(6)22, labsize(large) ) title("Blacks are No More Likely to Live in Large or Small Districts" )
					yline(0, lc(black) ) ylabe(-.15(.075).15)
					graphregion(color(white)) xtitle("Bandwidth", size(large) ) legend(off)
					ytitle("RD Estimate (${cilabel}% CI) ", size(large) ) name(needmove)
					
		;
#delimit cr




graph combine lev needmove, graphregion( color(white) fcolor(white) ) rows(2) ysize(5)
graph drop lev needmove

graph export "graphs/appendix/nodiscblack_bw.pdf", replace



/************
 Figure 22
*************/

/************************************************************************
Form Alternative dataset of outcomes of US House Contests
************************************************************************/

/*****
Get Running Variable
*****/

use "dta/stateleg/partisan_balance_lh.dta", clear

keep if red_election == 1

tempfile runvar
save `runvar'

/*****
Homogenize the Butler et al. data
*****/

use "raw data/lee et al elections/enricoall2.dta", clear

keep if year >= 1962

gen dem_win = demvote > repvote if !mi(demvote,repvote)

keep state district year demvoteshare dem_win

duplicates drop

rename district district_id
rename year federal_election_year

rename (demvoteshare dem_win state) mor_=

decode mor_state, gen(statename)

replace statename = substr(statename,5,.)
replace statename = regexr(statename, "N\.", "North")
replace statename = regexr(statename, "S\.", "South")
replace statename = regexr(statename, "W\.", "West")

gen year_redistrict = fed - mod(fed,10) + 1
replace year_red = year_red - 10 if mod(fed, 10) == 0


rename mor_dem_win dem_win

gen rep_win = 1-dem_win

merge m:1 statename using "dta/statecode_crosswalk.dta"
tab statename if _m == 1
keep if _m == 3
drop _m


tempfile moretti
save `moretti'



/*****
Bring in post-1992 outcomes
*****/

use "dta/gerrymander/ushouse.dta", clear


keep if ndem == 1 & nrep == 1

keep fed statename state_id dem_win rep_win year_red district_id

keep if fed > 1992

append using `moretti'


//We shift to make 1976-1980 attached to 1981
replace year_redistrict = year_redistrict + 10 ///
	if 	mod(federal_election_year,10) >= 6		///
	|	mod(federal_election_year,10) == 0

merge m:1 state_id year_red using `runvar'

keep if _m == 3
drop _m


/************************************************************************
Reform into a panel
************************************************************************/

/************
Unit Variable
************/
egen i = group(state_id year_red)

/************
Time Variable
************/

//We want time relative to redistricting year
gen relyear = federal_election_year - year_redistrict
gen t = relyear - mod(relyear+1, 2)



local out rep_win		//Outcome
local bw = 18			//Bandwidth
local deg = 1			//Polynomial degree
local run demhs2		//Running variable

preserve
//Sample: Elections in the specified year
keep if sample == 1 

//Cluster (for standard errors)
egen clust = group(year_redistrict state_id)

//Indicator for control of the assembly
gen control = (`run' >= 0) if !mi(`run')

//Restrict to bandwidth
keep if abs(`run') < `bw'

//Generate control function
forvalues p=1/`deg' {
	gen poly`p' = `run'^`p'
}

//Run regression on micro data
gen tmod = t+5
reg `out' i.tmod##c.(poly* control c.control#c.poly*) , cluster(clust)


clear
set obs 5
gen t 		= -5 + 2*(_n-1)
gen tmod 	= 2*(_n-1)


gen b 	= 0
gen se 	= 0
foreach tmod of numlist 2(2)8 {
	replace b 	= _b[`tmod'.tmod#c.control] 	if tmod == `tmod'
	replace se 	= _se[`tmod'.tmod#c.control] 	if tmod == `tmod'
}

local nclust = e(N_clust)

#delimit;
serrbar b se t, scale(`=invt(`nclust'-1, ${ci})') lp(dash dash) lc(blue blue)
					mvopt( mc(blue ) ms(S sh sh) )
					yline(0, lc(black) ) xline(0, lc(red) lp(dash) ) xline(2, lc(red) lp(dash) )
					legend(off) graphregion(color(white))
					xtitle("Years Until Redistricting", size(vlarge) )
					ytitle("Change in Probability Republican" "Wins when Democrats Take Control", size(large))
					xlabel(-5(2)3, labsize(vlarge))  ylabel(, labsize(vlarge))
					text(.18 1 "First Election" "After" "Redistricting", color(red) )
					text(.18 -2 "<------------Before Redistricting", color(gs8) )
					text(.18 2.5 "After-->", color(gs8) )
					;
#delimit cr

restore

graph export "graphs/appendix/lee_et_al_data.pdf", replace



/************
 Figure 23
*************/

local out rep_win		//Outcome
local bw = 18			//Bandwidth
local deg = 1			//Polynomial degree
local run demhs2		//Running variable

use "dta/gerrymander/election_level_house_qpanel.dta", clear

preserve
//Sample: Elections in the specified year
keep if sample == 1 

$twocandset

drop if statename == "Maine"


//keep if fed >= 1972


//Cluster (for standard errors)
egen clust = group(year_redistrict state_id)

//Indicator for control of the assembly
gen control = (`run' >= 0) if !mi(`run')

//Restrict to bandwidth
keep if abs(`run') < `bw'

//Generate control function
forvalues p=1/`deg' {
	gen poly`p' = `run'^`p'
}


//Run regression on micro data
gen tmod = t+5
reg `out' i.tmod##c.(poly* control c.control#c.poly*) , cluster(clust)


clear
set obs 5
gen t 		= -5 + 2*(_n-1)
gen tmod 	= 2*(_n-1)


gen b 	= 0
gen se 	= 0
foreach tmod of numlist 2(2)8 {
	replace b 	= _b[`tmod'.tmod#c.control] 	if tmod == `tmod'
	replace se 	= _se[`tmod'.tmod#c.control] 	if tmod == `tmod'
}

local nclust = e(N_clust)

#delimit;
serrbar b se t, scale(`=invt(`nclust'-1, ${ci})') lp(dash dash) lc(blue blue)
					mvopt( mc(blue ) ms(S sh sh) )
					yline(0, lc(black) ) xline(0, lc(red) lp(dash) ) xline(2, lc(red) lp(dash) )
					legend(off) graphregion(color(white))
					xtitle("Years Until Redistricting", size(vlarge) )
					ytitle("Change in Probability Republican" "Wins when Democrats Take Control", size(large))
					xlabel(-5(2)3, labsize(vlarge))  ylabel(, labsize(vlarge))
					text(.18 1 "First Election" "After" "Redistricting", color(red) )
					text(.18 -2 "<------------Before Redistricting", color(gs8) )
					text(.18 2.5 "After-->", color(gs8) )
					;
#delimit cr

restore


graph export "graphs/appendix/no_maine.pdf", replace




/************
 Table 5
*************/

/************************************************************************
Primary Data Set
************************************************************************/


use "dta/gerrymander/election_level_house_qpanel.dta", clear

keep if sample == 1 

${twocandset}

//Cluster (for standard errors)
egen clust 	= group(year_redistrict state_id)


reg rep_win demhs2 if !mi(demcon), cluster(clust)
keep if e(sample)


egen tag	= tag(clust)

//Share of seats won and chance of control
sum demhs2					if tag
local demhs2 				= string( r(mean), "%9.2f")
local demhs2sd				= string(r(sd), "%9.2f")

sum demcon					if tag
local demcon 				= string( r(mean), "%9.2f")

local keylist demhs2 demhs2sd demcon

foreach side in 1 0 {
	sum rep_win if demcon 	== `side'
	local repwin`side' 		= string(r(mean), "%9.2f")
	
	sum rep_share if demcon == `side'
	local repshare`side' 	= string(r(mean), "%9.2f")
	local repshare`side'sd 	= string(r(sd), "%9.2f")
	
	reg rep_win demhs2 if demcon == `side', cluster(clust)
	
	local nobs`side' 		= e(N)
	local nclust`side'		= e(N_clust)
	
	local keylist `keylist' repwin`side' repshare`side' repshare`side'sd nobs`side' nclust`side'

}



/************************************************************************
Open Elections
************************************************************************/
//We'll use the district panel for this, meaning only 1980 onwards
use "dta/gerrymander/district_panel_post.dta", clear
gen demcon = demhs2 >=0 if demhs2 < .
egen clust = group(year_redistrict state_id)

keep if open_consistent == 1

reg rep_win demhs2 if !mi(demcon), cluster(clust)
keep if e(sample)

foreach side in 1 0 {
	sum rep_win if demcon 			== `side'
	local repwin`side'open 			= string(r(mean), "%9.2f")
	
	sum rep_share if demcon 		== `side'
	local repshare`side'open 		= string(r(mean), "%9.2f")
	local repshare`side'sdopen 		= string(r(sd), "%9.2f")
	
	reg rep_win demhs2 if demcon 	== `side', cluster(clust)
	
	local nobs`side'open 			= e(N)
	local nclust`side'open			= e(N_clust)
	
	local keylist `keylist' repwin`side'open repshare`side'open repshare`side'sdopen nobs`side'open nclust`side'open


}


/************************************************************************
Output table using template
************************************************************************/
cap mkdir "tables"

//Base file:
tempfile desc0
filefilter "templates/descriptives_outcome.txt" "`desc0'", from(" ") to(" ")

//Loop
local onfile = 0
foreach key in `keylist' {
	local tmp = `onfile'+1
	tempfile desc`tmp'
	filefilter "`desc`onfile''" "`desc`tmp''", from("[`key']") to("``key''")
	local onfile = `tmp'
}

//The last temporary file we made is the final version
copy "`desc`tmp''" "tables/descriptives_outcome.tex", replace











/************************************************************************
*************************************************************************
District Demographics Descriptives Table
*************************************************************************
************************************************************************/
local keylist 



/************************************************************************
Fraction Black
************************************************************************/
local run 	demhs2
local bw	18
local binsize = 3
local deg = 1
local out distprered_fracblack



use "dta/gerrymander/districtcomp_preselect.dta", clear

//Cluster (for standard errors)
egen clust = group(year_redistrict state_id)

//Indicator for control of the assembly
gen control = (`run' >= 0) if !mi(`run')

keep if sample == 1

//Generate control function
forvalues p=1/`deg' {
	gen poly`p' = `run'^`p'
}


//Run regression on micro data
reg `out' poly* control c.control#c.poly* , cluster(clust)
keep if e(sample)

sum `out'
local `out' 				= string( r(mean), "%9.2f")
local `out'sd				= string(r(sd), "%9.2f")

local keylist `keylist' `out' `out'sd

/************************************************************************
Are African Americans more likely to have to be redistricted?
************************************************************************/
local run 	demhs2
local bw	18
local out distprered_absdev
local binsize = 3
local deg = 1


use "dta/gerrymander/districtcomp_needredistrict.dta", clear


keep if sample == 1

gen blackfrac	= b18ab/av0aa
gen blacktract 	=  blackfrac > .5 if !mi(blackfrac)



//Cluster (for standard errors)
egen clust = group(year_redistrict state_id)

//Indicator for control of the assembly
gen control = (`run' >= 0) if !mi(`run')

//Generate control function
forvalues p=1/`deg' {
	gen poly`p' = `run'^`p'
}


//Run regression on micro data
reg `out' poly* control c.control#c.poly* , cluster(clust)

keep if e(sample)

foreach black in 1 0 {
	sum `out' if blacktract == `black'
	local `out'`black' 				= string( r(mean), "%9.2f")
	local `out'`black'sd				= string(r(sd), "%9.2f")
	
	local keylist `keylist' `out'`black' `out'`black'sd
}





/************************************************************************
Do African-Americans Get Moved More Often?
************************************************************************/


local run 	demhs2
local bw	18
local samp 1
local out 

local binsize = 3
local deg = 1

local thresh .5



use "dta/gerrymander/frac_moved.dta", clear

egen clust = group(year_redistrict state_id)

//Regressions run on these will implicitly condition on whether the tract is
//majority black
gen frac_unfamiliar_black = frac_unfamiliar if (b18ab/av0aa > .5 & b18ab/ av0aa <= 1)
gen frac_unfamiliar_other = frac_unfamiliar if (b18ab/av0aa < .5 & b18ab/ av0aa <= 1)

gen moved_black			  = frac_unfamiliar_black > `thresh' if frac_unfamiliar_black < .
gen moved_other			  = frac_unfamiliar_other > `thresh' if frac_unfamiliar_other < .



//Pre-redistricting (placebo) or post?
keep if sample == 1 // & (year < 1990 | year > 1992) 


gen control = (`run' >= 0) if !mi(`run')
forvalues p=1/`deg' {
	gen poly`p' = `run'^`p'
}



foreach type in black other {
	local out moved_`type'
	
	preserve
	reg `out' poly* control c.control#c.poly* , cluster(clust)
	keep if e(sample)
	
	local nobs`type' 	= e(N)
	local nclust`type'	= e(N_clust)
	
	sum `out'
	local `out' 				= string( r(mean), "%9.2f")
	local `out'sd				= string(r(sd), "%9.2f")
	
	
	local keylist `keylist' `out' `out'sd nobs`type' nclust`type'
	
	restore
}

/************************************************************************
Output table using template
************************************************************************/
cap mkdir "tables"

//Base file:
tempfile desc0
filefilter "templates/descriptives_demo.txt" "`desc0'", from(" ") to(" ")

//Loop
local onfile = 0
foreach key in `keylist' {
	local tmp = `onfile'+1
	tempfile desc`tmp'
	filefilter "`desc`onfile''" "`desc`tmp''", from("[`key']") to("``key''")
	local onfile = `tmp'
}

//The last temporary file we made is the final version
copy "`desc`tmp''" "tables/descriptives_demo.tex", replace



/************
 Table 6
*************/
// which states appeaer for each redistricting year without restricting the bandwidth
	use "dta/gerrymander/election_level_house.dta", clear
	
	keep if sample == 1 
	$twocandset
	
	keep year_redistrict statename demhs2
	duplicates drop
	drop demhs2
	gen appear = 1
	
	reshape wide appear, i(statename) j(year_redistrict)
	rename appear* y*
	foreach var of varlist y1971-y2011{
	replace `var' = 0 if mi(`var')
	}
	tempfile state_indata
	save `state_indata'


// which states appeaer for each redistricting year within 18% seat share
	use "dta/gerrymander/election_level_house.dta", clear
	keep if sample == 1 
	$twocandset
	keep year_redistrict statename demhs2
	duplicates drop
	gen b = year_redistrict if abs(demhs2) < 18
	drop demhs2
	reshape wide b, i(statename) j(year_redistrict)
	foreach var of varlist b1971-b2011{
		replace `var' = 1 if !mi(`var')
		replace `var' = 0 if mi(`var')
	}
	merge 1:1 statename using `state_indata'
	
	gen years_in_bw 	= ""
	gen years_overall 	= ""
	foreach y of numlist 1971(10)2011 {
		replace years_in_bw 	= years_in_bw+",`y'" if b`y'
		replace years_overall 	= years_overall+",`y'" if y`y'
	}
	
	replace years_in_bw		= substr(years_in_bw,2,.)
	replace years_overall	= substr(years_overall,2,.)
	
	keep statename years_*
	sort statename
	
	cap file close allsamp 
	file open allsamp using "tables/app_allstates.tex", replace write
	
	file write allsamp "\begin{tabular}{lll}" _n
	file write allsamp "\hline \hline" _n
	file write allsamp "\textbf{State}  &  \textbf{Cycles in Sample}  &  \textbf{Cycles in BW} \\ " _n
	file write allsamp "\hline " _n
	
	forvalues i=1/`=_N' {
		local state = statename in `i'
		local yall	= years_overall in `i'
		local ybw	= years_in_bw in `i'
		
		file write allsamp "`state' & `yall' & `ybw' \\ " _n
	}
	
	file write allsamp "\hline \hline" _n
	file write allsamp "\end{tabular}"
	
	file close allsamp 
	

/*
graphs/background/partylines
graphs/background/redistricting_cycle_mod2
graphs/electionresults/racelev_evolution
graphs/appendix/racelev_bw
graphs/electionresults/race_lev_share_evolution
graphs/electionresults/dif_in_disc
graphs/boundaryshift/conversion_rate
graphs/electionresults/recent
graphs/sim/example
graphs/sim/multisim_2

graphs/appendix/rv_hist_freq
graphs/appendix/repshare_state
graphs/boundaryshift/unfamiliar
graphs/boundaryshift/overwhelming
graphs/boundaryshift/nodiscblack

graphs/background/density_small
graphs/boundaryshift/gerrydens

graphs/appendix/selection_bw
graphs/appendix/unfamiliar_bw
graphs/appendix/packblack_bw
graphs/appendix/blackden_bw
graphs/appendix/nodiscblack_bw
graphs/appendix/lee_et_al_data
graphs/appendix/no_maine

1 - table
tabselection
tabmainresult
tabrobustness
tabbandwidth
tabracial
tabdescoutcome
tabdescdemo
tables/app_allstates

regressions/selection_small.tex
regressions/mainresult.tex
regressions/robustness.tex
regressions/bandwidth.tex
regressions/racialgerrymandering.tex
tables/descriptives_outcome.tex
tables/descriptives_demo.tex
tables/app_allstates



